Skip to content

Commit

Permalink
[Mach-O] Do not create a baserel for a subtracted relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jul 6, 2022
1 parent be5fe4c commit a4cc7f3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
9 changes: 4 additions & 5 deletions macho/arch-arm64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ read_relocations(Context<E> &ctx, ObjectFile<E> &file,

Relocation<E> &rel = vec.back();

if (rels[i].type == ARM64_RELOC_SUBTRACTOR ||
(i > 0 && rels[i - 1].type == ARM64_RELOC_SUBTRACTOR)) {
rel.is_pcrel = false;
} else {
if (i > 0 && rels[i - 1].type == ARM64_RELOC_SUBTRACTOR)
rel.is_subtracted = true;

if (!rel.is_subtracted && rels[i].type != ARM64_RELOC_SUBTRACTOR)
rel.is_pcrel = r.is_pcrel;
}

if (r.is_extern) {
rel.sym = file.syms[r.idx];
Expand Down
9 changes: 4 additions & 5 deletions macho/arch-x86-64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ read_relocations(Context<E> &ctx, ObjectFile<E> &file,
vec.push_back({r.offset, (u8)r.type, (u8)r.p2size});
Relocation<E> &rel = vec.back();

if (rels[i].type == X86_64_RELOC_SUBTRACTOR ||
(i > 0 && rels[i - 1].type == X86_64_RELOC_SUBTRACTOR)) {
rel.is_pcrel = false;
} else {
if (i > 0 && rels[i - 1].type == X86_64_RELOC_SUBTRACTOR)
rel.is_subtracted = true;

if (!rel.is_subtracted && rels[i].type != X86_64_RELOC_SUBTRACTOR)
rel.is_pcrel = r.is_pcrel;
}

if (r.is_extern) {
rel.sym = file.syms[r.idx];
Expand Down
1 change: 1 addition & 0 deletions macho/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Relocation {
u8 type = -1;
u8 p2size = 0;
bool is_pcrel : 1 = false;
bool is_subtracted : 1 = false;
bool needs_dynrel : 1 = false;
i64 addend = 0;
Symbol<E> *sym = nullptr;
Expand Down
3 changes: 2 additions & 1 deletion macho/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,8 @@ void RebaseSection<E>::compute_size(Context<E> &ctx) {
if (chunk->is_output_section && !chunk->hdr.match("__TEXT", "__eh_frame"))
for (Subsection<E> *subsec : ((OutputSection<E> *)chunk)->members)
for (Relocation<E> &rel : subsec->get_rels())
if (!rel.is_pcrel && rel.type == E::abs_rel && !refers_tls(rel.sym))
if (!rel.is_pcrel && !rel.is_subtracted && rel.type == E::abs_rel &&
!refers_tls(rel.sym))
enc.add(seg->seg_idx,
subsec->get_addr(ctx) + rel.offset - seg->cmd.vmaddr);

Expand Down

0 comments on commit a4cc7f3

Please sign in to comment.