Bug 1839746 - Make elfhack generate and handle a more standard SHT_RELR section. r=gsvelto

Bug 1747782 changed the format to SHT_RELR, but what's produced is still
different from a real SHT_RELR section, because it uses a terminating
nul entry.

Differential Revision: https://phabricator.services.mozilla.com/D181689
This commit is contained in:
Mike Hommey 2023-06-22 23:56:47 +00:00
parent 51cd350729
commit 01b58f59f2
2 changed files with 8 additions and 4 deletions

View File

@ -59,6 +59,9 @@ class ElfRelHack_Section : public ElfSection {
void serialize(std::ofstream& file, unsigned char ei_class,
unsigned char ei_data) {
if (bitmap) {
relr.push_back((bitmap << 1) | 1);
}
for (std::vector<Elf64_Addr>::iterator i = relr.begin(); i != relr.end();
++i) {
Elf_Addr out;
@ -101,7 +104,7 @@ class ElfRelHack_Section : public ElfSection {
bitmap |= 1ULL << ((offset - block_start) / shdr.sh_entsize);
break;
}
shdr.sh_size = relr.size() * shdr.sh_entsize;
shdr.sh_size = (relr.size() + (bitmap ? 1 : 0)) * shdr.sh_entsize;
}
private:
@ -469,6 +472,8 @@ class ElfRelHackCode_Section : public ElfSection {
if (symtab->syms[ELF64_R_SYM(r->r_info)].value.getSection() == nullptr) {
if (strcmp(name, "relhack") == 0) {
addr = relhack_section.getAddr();
} else if (strcmp(name, "relhack_end") == 0) {
addr = relhack_section.getAddr() + relhack_section.getSize();
} else if (strcmp(name, "elf_header") == 0) {
// TODO: change this ungly hack to something better
ElfSection* ehdr = parent.getSection(1)->getPrevious()->getPrevious();
@ -1018,8 +1023,6 @@ int do_relocation_section(Elf* elf, unsigned int rel_type,
relhack->push_back(i->r_offset);
}
}
// Last entry must be a nullptr
relhack->push_back(0);
if (init_array) {
// Some linkers create a DT_INIT_ARRAY section that, for all purposes,

View File

@ -68,6 +68,7 @@ extern __attribute__((visibility("hidden"))) void original_init(int argc,
char** env);
extern __attribute__((visibility("hidden"))) Elf_Addr relhack[];
extern __attribute__((visibility("hidden"))) Elf_Addr relhack_end[];
extern __attribute__((visibility("hidden"))) Elf_Ehdr elf_header;
extern __attribute__((visibility("hidden"))) int (*mprotect_cb)(void* addr,
@ -79,7 +80,7 @@ extern __attribute__((visibility("hidden"))) char relro_end[];
static inline __attribute__((always_inline)) void do_relocations(void) {
Elf_Addr* ptr;
for (Elf_Addr* entry = relhack; *entry; entry++) {
for (Elf_Addr* entry = relhack; entry < relhack_end; entry++) {
if ((*entry & 1) == 0) {
ptr = (Elf_Addr*)((intptr_t)&elf_header + *entry);
*ptr += (intptr_t)&elf_header;