Skip to content

Commit

Permalink
options/rtld: Fix relr relocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Oct 17, 2024
1 parent 5336ba3 commit be62c6e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions options/rtld/generic/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,11 @@ void Loader::_processStaticRelocations(SharedObject *object) {
*addr++ += object->baseAddress;
}else {
// Odd entry indicates entry is a bitmap of the subsequent locations to be relocated.

// The first bit of an entry is always a marker about whether the entry is an address or a bitmap,
// discard it.
entry >>= 1;

for(int i = 0; entry; ++i) {
if(entry & 1) {
addr[i] += object->baseAddress;
Expand Down
5 changes: 5 additions & 0 deletions options/rtld/generic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ extern "C" void relocateSelf() {
*addr++ += ldso_base;
}else {
// Odd entry indicates entry is a bitmap of the subsequent locations to be relocated.

// The first bit of an entry is always a marker about whether the entry is an address or a bitmap,
// discard it.
entry >>= 1;

for(int i = 0; entry; ++i) {
if(entry & 1) {
addr[i] += ldso_base;
Expand Down
1 change: 1 addition & 0 deletions tests/rtld/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rtld_test_cases = [
'scope4',
'scope5',
'tls_align',
'relr',
]

host_libc_rtld_nosan_test_cases = [
Expand Down
1 change: 1 addition & 0 deletions tests/rtld/relr/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_additional_link_args += ['-Wl,-z,pack-relative-relocs']
24 changes: 24 additions & 0 deletions tests/rtld/relr/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stddef.h>
#include <assert.h>

const char* strings[] = {
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
};

int main() {
for(size_t i = 0; i < sizeof(strings) / sizeof(*strings); ++i) {
assert(strings[i][0] == 0);
}

return 0;
}

0 comments on commit be62c6e

Please sign in to comment.