Skip to content

Commit 1870845

Browse files
committed
Refactor
1 parent d15c41a commit 1870845

File tree

5 files changed

+66
-74
lines changed

5 files changed

+66
-74
lines changed

src/cmdline.cc

+6-8
Original file line numberDiff line numberDiff line change
@@ -896,15 +896,13 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
896896
} else if (read_arg("soname") || read_arg("h")) {
897897
ctx.arg.soname = arg;
898898
} else if (read_arg("audit")) {
899-
if (ctx.arg.audit.empty())
900-
ctx.arg.audit = arg;
901-
else
902-
ctx.arg.audit += ":" + std::string(arg);
899+
if (!ctx.arg.audit.empty())
900+
ctx.arg.audit += ':';
901+
ctx.arg.audit += std::string(arg);
903902
} else if (read_arg("depaudit") || read_arg("P")) {
904-
if (ctx.arg.depaudit.empty())
905-
ctx.arg.depaudit = arg;
906-
else
907-
ctx.arg.depaudit += ":" + std::string(arg);
903+
if (!ctx.arg.depaudit.empty())
904+
ctx.arg.depaudit += ':';
905+
ctx.arg.depaudit += std::string(arg);
908906
} else if (read_flag("allow-multiple-definition")) {
909907
ctx.arg.allow_multiple_definition = true;
910908
} else if (read_flag("apply-dynamic-relocs")) {

src/input-files.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
409409
continue;
410410

411411
// Ignore section is specified by --discard-section.
412-
if (ctx.arg.discard_section.contains(name))
412+
if (!ctx.arg.discard_section.empty() &&
413+
ctx.arg.discard_section.contains(name))
413414
continue;
414415

415416
if (name == ".comment" &&
@@ -594,6 +595,8 @@ void ObjectFile<E>::parse_ehframe(Context<E> &ctx) {
594595
i64 cie_offset = *(I32<E> *)(contents.data() + fdes[i].input_offset + 4);
595596
fdes[i].cie_idx = find_cie(fdes[i].input_offset + 4 - cie_offset);
596597
}
598+
599+
isec->is_alive = false;
597600
}
598601

599602
auto get_isec = [&](const FdeRecord<E> &fde) {

src/output-chunks.cc

+33-32
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ void OutputSection<E>::compute_section_size(Context<E> &ctx) {
865865
std::span<InputSection<E> *> members;
866866
i64 size = 0;
867867
i64 offset = 0;
868+
i64 align = 1;
868869
};
869870

870871
std::vector<Group> groups;
@@ -876,26 +877,28 @@ void OutputSection<E>::compute_section_size(Context<E> &ctx) {
876877
m = m.subspan(sz);
877878
}
878879

879-
tbb::parallel_for_each(groups, [](Group &group) {
880+
tbb::parallel_for_each(groups, [](Group &g) {
880881
i64 off = 0;
881-
for (InputSection<E> *isec : group.members)
882+
for (InputSection<E> *isec : g.members) {
882883
off = align_to(off, 1 << isec->p2align) + isec->sh_size;
883-
group.size = off;
884+
g.align = std::max<i64>(g.align, 1 << isec->p2align);
885+
}
886+
g.size = off;
884887
});
885888

886889
i64 off = 0;
887-
for (i64 i = 0; i < groups.size(); i++) {
888-
off = align_to(off, shdr.sh_addralign);
889-
groups[i].offset = off;
890-
off += groups[i].size;
890+
for (Group &g : groups) {
891+
off = align_to(off, g.align);
892+
g.offset = off;
893+
off += g.size;
891894
}
892895

893896
shdr.sh_size = off;
894897

895898
// Assign offsets to input sections.
896-
tbb::parallel_for_each(groups, [](Group &group) {
897-
i64 off = group.offset;
898-
for (InputSection<E> *isec : group.members) {
899+
tbb::parallel_for_each(groups, [](Group &g) {
900+
i64 off = g.offset;
901+
for (InputSection<E> *isec : g.members) {
899902
off = align_to(off, 1 << isec->p2align);
900903
isec->offset = off;
901904
off += isec->sh_size;
@@ -932,7 +935,7 @@ void OutputSection<E>::copy_buf(Context<E> &ctx) {
932935
P -= delta;
933936
}
934937

935-
auto dynrel = [&](i64 ty, i64 idx, u64 val) {
938+
auto write = [&](i64 ty, i64 idx, u64 val) {
936939
*rel++ = ElfRel<E>(P, ty, idx, val);
937940
if (ctx.arg.apply_dynamic_relocs)
938941
*(Word<E> *)loc = val;
@@ -944,14 +947,14 @@ void OutputSection<E>::copy_buf(Context<E> &ctx) {
944947
*(Word<E> *)loc = S + A;
945948
break;
946949
case ABS_REL_BASEREL:
947-
dynrel(E::R_RELATIVE, 0, S + A);
950+
write(E::R_RELATIVE, 0, S + A);
948951
break;
949952
case ABS_REL_IFUNC:
950953
if constexpr (supports_ifunc<E>)
951-
dynrel(E::R_IRELATIVE, 0, sym.get_addr(ctx, NO_PLT) + A);
954+
write(E::R_IRELATIVE, 0, sym.get_addr(ctx, NO_PLT) + A);
952955
break;
953956
case ABS_REL_DYNREL:
954-
dynrel(E::R_ABS, sym.get_dynsym_idx(ctx), A);
957+
write(E::R_ABS, sym.get_dynsym_idx(ctx), A);
955958
break;
956959
}
957960
}
@@ -1423,23 +1426,20 @@ void GotSection<E>::copy_buf(Context<E> &ctx) {
14231426
ent.sym ? ent.sym->get_dynsym_idx(ctx) : 0,
14241427
ent.val);
14251428

1426-
// A single TLSDESC relocation fixes two consecutive GOT slots
1427-
// where one slot holds a function pointer and the other an
1428-
// argument to the function. An addend should be applied not to
1429-
// the function pointer but to the function argument, which is
1430-
// usually stored to the second slot.
1431-
//
1432-
// ARM32 employs the inverted layout for some reason, so an
1433-
// addend is applied to the first slot.
1434-
bool is_tlsdesc = false;
1435-
if constexpr (supports_tlsdesc<E> && !is_arm32<E>)
1436-
is_tlsdesc = (ent.r_type == E::R_TLSDESC);
1437-
14381429
if (ctx.arg.apply_dynamic_relocs) {
1439-
if (is_tlsdesc)
1440-
buf[ent.idx + 1] = ent.val;
1441-
else
1442-
buf[ent.idx] = ent.val;
1430+
// A single TLSDESC relocation fixes two consecutive GOT slots
1431+
// where one slot holds a function pointer and the other an
1432+
// argument to the function. An addend should be applied not to
1433+
// the function pointer but to the function argument, which is
1434+
// usually stored to the second slot.
1435+
//
1436+
// ARM32 employs the inverted layout for some reason, so an
1437+
// addend is applied to the first slot.
1438+
i64 i = ent.idx;
1439+
if constexpr (supports_tlsdesc<E> && !is_arm32<E>)
1440+
if (ent.r_type == E::R_TLSDESC)
1441+
i = ent.idx + 1;
1442+
buf[i] = ent.val;
14431443
}
14441444
}
14451445
}
@@ -1721,8 +1721,9 @@ static u64 get_symbol_size(Symbol<E> &sym) {
17211721
if constexpr (is_riscv<E> || is_loongarch<E>)
17221722
if (esym.st_size)
17231723
if (InputSection<E> *isec = sym.get_input_section())
1724-
return esym.st_size + esym.st_value - sym.value -
1725-
get_r_delta(*isec, esym.st_value + esym.st_size);
1724+
if (isec->shdr().sh_flags & SHF_EXECINSTR)
1725+
return esym.st_size + esym.st_value - sym.value -
1726+
get_r_delta(*isec, esym.st_value + esym.st_size);
17261727
return esym.st_size;
17271728
}
17281729

src/passes.cc

+20-31
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ void parse_eh_frame_sections(Context<E> &ctx) {
378378

379379
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
380380
file->parse_ehframe(ctx);
381-
382-
for (InputSection<E> *isec : file->eh_frame_sections)
383-
isec->is_alive = false;
384381
});
385382
}
386383

@@ -415,13 +412,9 @@ void convert_common_symbols(Context<E> &ctx) {
415412

416413
template <typename E>
417414
static bool has_ctors_and_init_array(Context<E> &ctx) {
418-
bool x = false;
419-
bool y = false;
420-
for (ObjectFile<E> *file : ctx.objs) {
421-
x |= file->has_ctors;
422-
y |= file->has_init_array;
423-
}
424-
return x && y;
415+
return
416+
ranges::any_of(ctx.objs, [](ObjectFile<E> *x) { return x->has_ctors; }) &&
417+
ranges::any_of(ctx.objs, [](ObjectFile<E> *x) { return x->has_init_array; });
425418
}
426419

427420
template <typename E>
@@ -444,7 +437,6 @@ static u64 canonicalize_type(std::string_view name, u64 type) {
444437
if constexpr (is_x86_64<E>)
445438
if (type == SHT_X86_64_UNWIND)
446439
return SHT_PROGBITS;
447-
448440
return type;
449441
}
450442

@@ -1370,12 +1362,11 @@ void shuffle_sections(Context<E> &ctx) {
13701362
template <typename E>
13711363
void add_dynamic_strings(Context<E> &ctx) {
13721364
for (SharedFile<E> *file : ctx.dsos) {
1373-
std::string s = std::string(file->get_dt_audit(ctx));
1365+
std::string_view s = file->get_dt_audit(ctx);
13741366
if (!s.empty()) {
1375-
if (ctx.arg.depaudit.empty())
1376-
ctx.arg.depaudit = s;
1377-
else
1378-
ctx.arg.depaudit += ":" + s;
1367+
if (!ctx.arg.depaudit.empty())
1368+
ctx.arg.depaudit += ':';
1369+
ctx.arg.depaudit += std::string(s);
13791370
}
13801371
}
13811372

@@ -1402,7 +1393,9 @@ void compute_section_sizes(Context<E> &ctx) {
14021393
Timer t(ctx, "compute_section_sizes");
14031394

14041395
if constexpr (needs_thunk<E>) {
1405-
auto tail = ranges::partition(ctx.chunks, [&](Chunk<E> *chunk) {
1396+
std::vector<Chunk<E> *> vec = ctx.chunks;
1397+
1398+
auto tail = ranges::partition(vec, [&](Chunk<E> *chunk) {
14061399
return chunk->to_osec() && (chunk->shdr.sh_flags & SHF_EXECINSTR) &&
14071400
!ctx.arg.relocatable;
14081401
});
@@ -1411,7 +1404,7 @@ void compute_section_sizes(Context<E> &ctx) {
14111404
for (Chunk<E> *chunk : std::span(vec.begin(), tail.begin()))
14121405
chunk->to_osec()->create_range_extension_thunks(ctx, true);
14131406

1414-
tbb::parallel_for_each(tail.begin(), tail.end(), [&](Chunk<E> *chunk) {
1407+
tbb::parallel_for_each(tail, [&](Chunk<E> *chunk) {
14151408
chunk->compute_section_size(ctx);
14161409
});
14171410
} else {
@@ -1773,29 +1766,26 @@ void sort_dynsyms(Context<E> &ctx) {
17731766
return;
17741767

17751768
// In any symtab, local symbols must precede global symbols.
1776-
auto first_global = std::stable_partition(syms.begin() + 1, syms.end(),
1777-
[&](Symbol<E> *sym) {
1769+
auto globals = ranges::stable_partition(syms.subspan(1), [&](Symbol<E> *sym) {
17781770
return sym->is_local(ctx);
17791771
});
17801772

17811773
// .gnu.hash imposes more restrictions on the order of the symbols in
17821774
// .dynsym.
17831775
if (ctx.gnu_hash) {
1784-
auto first_exported = std::stable_partition(first_global, syms.end(),
1785-
[](Symbol<E> *sym) {
1776+
auto exported_syms = ranges::stable_partition(globals, [](Symbol<E> *sym) {
17861777
return !sym->is_exported;
17871778
});
17881779

17891780
// Count the number of exported symbols to compute the size of .gnu.hash.
1790-
i64 num_exported = syms.end() - first_exported;
1781+
i64 num_exported = exported_syms.size();
17911782
u32 num_buckets = num_exported / ctx.gnu_hash->LOAD_FACTOR + 1;
17921783

1793-
tbb::parallel_for_each(first_exported, syms.end(), [&](Symbol<E> *sym) {
1784+
tbb::parallel_for_each(exported_syms, [&](Symbol<E> *sym) {
17941785
sym->set_djb_hash(ctx, djb_hash(sym->name()));
17951786
});
17961787

1797-
tbb::parallel_sort(first_exported, syms.end(),
1798-
[&](Symbol<E> *a, Symbol<E> *b) {
1788+
tbb::parallel_sort(exported_syms, [&](Symbol<E> *a, Symbol<E> *b) {
17991789
return std::tuple(a->get_djb_hash(ctx) % num_buckets, a->name()) <
18001790
std::tuple(b->get_djb_hash(ctx) % num_buckets, b->name());
18011791
});
@@ -1816,7 +1806,7 @@ void sort_dynsyms(Context<E> &ctx) {
18161806
ctx.dynstr->shdr.sh_size += size.combine(std::plus());
18171807

18181808
// ELF's symbol table sh_info holds the offset of the first global symbol.
1819-
ctx.dynsym->shdr.sh_info = first_global - syms.begin();
1809+
ctx.dynsym->shdr.sh_info = globals.begin() - syms.begin();
18201810
}
18211811

18221812
template <typename E>
@@ -2709,11 +2699,10 @@ void separate_debug_sections(Context<E> &ctx) {
27092699
chunk->name.starts_with(".debug_");
27102700
};
27112701

2712-
auto mid = std::stable_partition(ctx.chunks.begin(), ctx.chunks.end(),
2713-
is_debug_section);
2702+
auto tail = ranges::stable_partition(ctx.chunks, std::not_fn(is_debug_section));
27142703

2715-
ctx.debug_chunks = {ctx.chunks.begin(), mid};
2716-
ctx.chunks.erase(ctx.chunks.begin(), mid);
2704+
ctx.debug_chunks = {tail.begin(), tail.end()};
2705+
ctx.chunks.erase(tail.begin(), tail.end());
27172706
}
27182707

27192708
template <typename E>

src/shrink-sections.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ void shrink_sections(Context<E> &ctx) {
9999
for (Symbol<E> *sym : file->symbols)
100100
if (sym->file == file)
101101
if (InputSection<E> *isec = sym->get_input_section())
102-
if (i64 delta = get_r_delta(*isec, sym->value))
103-
sym->value -= delta;
102+
if (isec->shdr().sh_flags & SHF_EXECINSTR)
103+
if (i64 delta = get_r_delta(*isec, sym->value))
104+
sym->value -= delta;
104105
});
105106

106107
// Recompute sizes of executable sections

0 commit comments

Comments
 (0)