Skip to content

Commit

Permalink
[ELF] Fix assertion failure
Browse files Browse the repository at this point in the history
This patch fixes a regression introduced in
43fa021.

Fixes rui314#259
  • Loading branch information
rui314 committed Jan 6, 2022
1 parent 79e397e commit 2f86fef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 12 additions & 3 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,21 @@ void scan_rels(Context<E> &ctx) {
});

std::vector<Symbol<E> *> syms = flatten(vec);
ctx.symbol_aux.reserve(syms.size());

auto add_aux = [&](Symbol<E> *sym) {
if (sym->aux_idx == -1) {
i64 sz = ctx.symbol_aux.size();
sym->aux_idx = sz;
ctx.symbol_aux.resize(sz + 1);
}
};

ctx.symbol_aux.resize(syms.size());
for (i64 i = 0; i < syms.size(); i++)
syms[i]->aux_idx = i;

// Assign offsets in additional tables for each dynamic symbol.
for (Symbol<E> *sym : syms) {
add_aux(sym);

if (sym->is_imported || sym->is_exported)
ctx.dynsym->add_symbol(ctx, sym);

Expand Down Expand Up @@ -583,6 +591,7 @@ void scan_rels(Context<E> &ctx) {
// Aliases of this symbol are also copied so that they will be
// resolved to the same address at runtime.
for (Symbol<E> *alias : file->find_aliases(sym)) {
add_aux(alias);
alias->is_imported = true;
alias->is_exported = true;
alias->has_copyrel = true;
Expand Down
13 changes: 6 additions & 7 deletions test/elf/copyrel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ int main() {
}
EOF

cat <<EOF | cc -o "$t"/b.o -c -x assembler -
.globl foo, bar
.data;
foo:
bar:
.long 42
cat <<EOF | cc -fPIC -o "$t"/b.o -c -xc -
int foo = 42;
extern int bar __attribute__((alias("foo")));
extern int baz __attribute__((alias("foo")));
EOF

clang -fuse-ld="$mold" -no-pie -o "$t"/exe "$t"/a.o "$t"/b.o
clang -fuse-ld="$mold" -shared -o "$t"/c.so "$t"/b.o
clang -fuse-ld="$mold" -no-pie -o "$t"/exe "$t"/a.o "$t"/c.so
"$t"/exe | grep -q '42 42 1'

echo OK

0 comments on commit 2f86fef

Please sign in to comment.