Skip to content

Commit bdb00bc

Browse files
committed
Simplify
1 parent abcbd45 commit bdb00bc

7 files changed

Lines changed: 38 additions & 53 deletions

File tree

src/arch-ppc64v1.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ void Thunk<E>::copy_buf(Context<E> &ctx) {
486486

487487
static InputSection<E> *get_opd_section(ObjectFile<E> &file) {
488488
for (std::unique_ptr<InputSection<E>> &isec : file.sections)
489-
if (isec && isec->name() == ".opd")
489+
if (isec && isec->name == ".opd")
490490
return isec.get();
491491
return nullptr;
492492
}

src/gc-sections.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@ template <typename E>
1515
static bool should_keep(const InputSection<E> &isec) {
1616
u32 type = isec.shdr().sh_type;
1717
u32 flags = isec.shdr().sh_flags;
18-
std::string_view name = isec.name();
19-
2018
if constexpr (is_ppc32<E>)
21-
if (name == ".got2")
19+
if (isec.name == ".got2")
2220
return true;
2321

2422
return (flags & SHF_GNU_RETAIN) ||
2523
type == SHT_NOTE ||
2624
type == SHT_INIT_ARRAY ||
2725
type == SHT_FINI_ARRAY ||
2826
type == SHT_PREINIT_ARRAY ||
29-
name.starts_with(".ctors") ||
30-
name.starts_with(".dtors") ||
31-
name.starts_with(".init") ||
32-
name.starts_with(".fini");
27+
isec.name.starts_with(".ctors") ||
28+
isec.name.starts_with(".dtors") ||
29+
isec.name.starts_with(".init") ||
30+
isec.name.starts_with(".fini");
3331
}
3432

3533
// Sections whose names are valid C identifiers can be referenced via
@@ -47,8 +45,8 @@ static StartStopMap<E> build_start_stop_map(Context<E> &ctx) {
4745
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
4846
for (std::unique_ptr<InputSection<E>> &isec : file->sections)
4947
if (isec && isec->is_alive && (isec->shdr().sh_flags & SHF_ALLOC) &&
50-
is_c_identifier(isec->name()))
51-
map.insert({isec->name(), isec.get()});
48+
is_c_identifier(isec->name))
49+
map.insert({isec->name, isec.get()});
5250
});
5351
return map;
5452
}

src/icf.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,24 @@ static void uniquify_cies(Context<E> &ctx) {
123123
template <typename E>
124124
static bool is_eligible(Context<E> &ctx, InputSection<E> &isec) {
125125
const ElfShdr<E> &shdr = isec.shdr();
126-
std::string_view name = isec.name();
127-
126+
128127
if (shdr.sh_size == 0 || !(shdr.sh_flags & SHF_ALLOC) ||
129-
shdr.sh_type == SHT_NOBITS || is_c_identifier(name))
128+
shdr.sh_type == SHT_NOBITS || is_c_identifier(isec.name))
130129
return false;
131130

132131
if (shdr.sh_flags & SHF_EXECINSTR)
133132
return (ctx.arg.icf_all || !isec.address_taken) &&
134-
name != ".init" && name != ".fini";
133+
isec.name != ".init" && isec.name != ".fini";
135134

136135
// .gcc_except_table contains a compiler-generated table. Pointer
137136
// equality for the section is not significant because only the C++
138137
// exception handling code will use the table at runtime.
139-
if (name == ".gcc_except_table" || name.starts_with(".gcc_except_table."))
138+
if (isec.name == ".gcc_except_table" ||
139+
isec.name.starts_with(".gcc_except_table."))
140140
return true;
141141

142142
bool is_readonly = !(shdr.sh_flags & SHF_WRITE);
143-
bool is_relro = isec.name().starts_with(".data.rel.ro");
143+
bool is_relro = isec.name.starts_with(".data.rel.ro");
144144
return (ctx.arg.ignore_data_address_equality || !isec.address_taken) &&
145145
(is_readonly || is_relro);
146146
}

src/input-files.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ void ObjectFile<E>::convert_mergeable_sections(Context<E> &ctx) {
747747
continue;
748748

749749
MergedSection<E> *parent =
750-
MergedSection<E>::get_instance(ctx, isec->name(), shdr);
750+
MergedSection<E>::get_instance(ctx, isec->name, shdr);
751751

752752
if (parent) {
753753
this->mergeable_sections[i] =

src/input-sections.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ template <typename E>
3434
InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file, i64 shndx)
3535
: file(file), shndx(shndx) {
3636
if (shndx < file.elf_sections.size()) {
37+
name = file.shstrtab.data() + file.elf_sections[shndx].sh_name;
3738
contents = {(char *)file.mf->data + shdr().sh_offset, (size_t)shdr().sh_size};
38-
const char *p = file.shstrtab.data() + file.elf_sections[shndx].sh_name;
39-
cached_name = std::string_view(p, strlen(p));
4039
} else {
41-
cached_name = (shdr().sh_flags & SHF_TLS)
42-
? std::string_view(".tls_common", 11)
43-
: std::string_view(".common", 7);
40+
name = (shdr().sh_flags & SHF_TLS) ? ".tls_common" : ".common";
4441
}
4542

4643
if (shdr().sh_flags & SHF_COMPRESSED) {

src/mold.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,6 @@ class __attribute__((aligned(4))) InputSection {
514514
void apply_reloc_nonalloc(Context<E> &ctx, u8 *base);
515515
void kill();
516516

517-
std::string_view name() const;
518517
i64 get_priority() const;
519518
u64 get_addr() const;
520519
ElfShdr<E> &shdr() const;
@@ -533,11 +532,9 @@ class __attribute__((aligned(4))) InputSection {
533532
OutputSection<E> *output_section = nullptr;
534533
i64 sh_size = -1;
535534

535+
std::string_view name;
536536
std::string_view contents;
537537

538-
// Cached to avoid per-call strlen on the shstrtab entry.
539-
std::string_view cached_name;
540-
541538
i32 fde_begin = -1;
542539
i32 fde_end = -1;
543540

@@ -2932,7 +2929,7 @@ std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym);
29322929
template <typename E>
29332930
inline std::ostream &
29342931
operator<<(std::ostream &out, const InputSection<E> &isec) {
2935-
out << isec.file << ":(" << isec.name() << ")";
2932+
out << isec.file << ":(" << isec.name << ")";
29362933
return out;
29372934
}
29382935

@@ -2948,11 +2945,6 @@ inline u64 InputSection<E>::get_addr() const {
29482945
return output_section->shdr.sh_addr + offset;
29492946
}
29502947

2951-
template <typename E>
2952-
inline std::string_view InputSection<E>::name() const {
2953-
return cached_name;
2954-
}
2955-
29562948
template <typename E>
29572949
inline i64 InputSection<E>::get_priority() const {
29582950
return ((i64)file.priority << 32) | shndx;
@@ -3046,20 +3038,19 @@ InputSection<E>::get_tombstone(Symbol<E> &sym, SectionFragment<E> *frag) {
30463038
if (!isec || isec->is_alive)
30473039
return {};
30483040

3049-
std::string_view str = name();
3050-
if (!str.starts_with(".debug_"))
3041+
if (!name.starts_with(".debug_"))
30513042
return {};
30523043

30533044
// If the section was dead due to ICF, we don't want to emit debug
30543045
// info for that section but want to set real values to .debug_line so
30553046
// that users can set a breakpoint inside a merged section.
3056-
if (isec->icf_removed() && str == ".debug_line")
3047+
if (isec->icf_removed() && name == ".debug_line")
30573048
return {};
30583049

30593050
// 0 is an invalid value in most debug info sections, so we use it
30603051
// as a tombstone value. .debug_loc and .debug_ranges reserve 0 as
3061-
// the terminator marker, so we use 1 if that'str the case.
3062-
return (str == ".debug_loc" || str == ".debug_ranges") ? 1 : 0;
3052+
// the terminator marker, so we use 1 if that's the case.
3053+
return (name == ".debug_loc" || name == ".debug_ranges") ? 1 : 0;
30633054
}
30643055

30653056
template <typename E>
@@ -3194,7 +3185,7 @@ u64 Symbol<E>::get_addr(Context<E> &ctx, i64 flags) const {
31943185
if (isec->icf_removed())
31953186
return isec->leader->get_addr() + value;
31963187

3197-
if (isec->name() == ".eh_frame") {
3188+
if (isec->name == ".eh_frame") {
31983189
// .eh_frame contents are parsed and reconstructed by the linker,
31993190
// so pointing to a specific location in a source .eh_frame
32003191
// section doesn't make much sense. However, CRT files contain

src/passes.cc

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -576,15 +576,14 @@ get_output_section_key(Context<E> &ctx, InputSection<E> &isec,
576576
// We do not place them into .init_array/.fini_array because such
577577
// invalid pointer values would simply make the program to crash.
578578
if (ctors_in_init_array && !isec.get_rels(ctx).empty()) {
579-
std::string_view name = isec.name();
580-
if (name == ".ctors" || name.starts_with(".ctors."))
579+
if (isec.name == ".ctors" || isec.name.starts_with(".ctors."))
581580
return {".init_array", SHT_INIT_ARRAY};
582-
if (name == ".dtors" || name.starts_with(".dtors."))
581+
if (isec.name == ".dtors" || isec.name.starts_with(".dtors."))
583582
return {".fini_array", SHT_FINI_ARRAY};
584583
}
585584

586585
const ElfShdr<E> &shdr = isec.shdr();
587-
std::string_view name = get_output_name(ctx, isec.name(), shdr.sh_flags);
586+
std::string_view name = get_output_name(ctx, isec.name, shdr.sh_flags);
588587
u64 type = canonicalize_type<E>(name, shdr.sh_type);
589588
return {name, type};
590589
}
@@ -645,7 +644,7 @@ void create_output_sections(Context<E> &ctx) {
645644
~SHF_COMPRESSED & ~SHF_GNU_RETAIN;
646645

647646
if (ctx.arg.relocatable && (sh_flags & SHF_GROUP)) {
648-
OutputSection<E> *osec = new OutputSection<E>(isec->name(), shdr.sh_type);
647+
OutputSection<E> *osec = new OutputSection<E>(isec->name, shdr.sh_type);
649648
osec->sh_flags = sh_flags;
650649
isec->output_section = osec;
651650
ctx.osec_pool.emplace_back(osec);
@@ -1298,7 +1297,7 @@ void check_symbol_types(Context<E> &ctx) {
12981297
template <typename E>
12991298
static i64 get_init_fini_priority(InputSection<E> *isec) {
13001299
static std::regex re(R"(\.(\d+)$)", std::regex_constants::optimize);
1301-
std::string_view name = isec->name();
1300+
std::string_view name = isec->name;
13021301
std::cmatch m;
13031302
if (std::regex_search(name.data(), name.data() + name.size(), m, re))
13041303
return std::stoi(m[1]);
@@ -1315,14 +1314,14 @@ static i64 get_ctor_dtor_priority(InputSection<E> *isec) {
13151314
// crtbegin.o and crtend.o contain marker symbols such as
13161315
// __CTOR_LIST__ or __DTOR_LIST__. So they have to be at the
13171316
// beginning or end of the section.
1318-
std::smatch m;
1319-
if (std::regex_search(isec->file.filename, m, re1))
1317+
if (std::regex_search(isec->file.filename, re1))
13201318
return -2;
1321-
if (std::regex_search(isec->file.filename, m, re2))
1319+
if (std::regex_search(isec->file.filename, re2))
13221320
return 65536;
13231321

1324-
std::string name(isec->name());
1325-
if (std::regex_search(name, m, re3))
1322+
std::string_view name = isec->name;
1323+
std::cmatch m;
1324+
if (std::regex_search(name.data(), name.data() + name.size(), m, re3))
13261325
return std::stoi(m[1]);
13271326
return -1;
13281327
}
@@ -1346,8 +1345,8 @@ void sort_init_fini(Context<E> &ctx) {
13461345
std::vector<Entry> vec;
13471346

13481347
for (InputSection<E> *isec : osec->members) {
1349-
std::string_view name = isec->name();
1350-
if (name.starts_with(".ctors") || name.starts_with(".dtors"))
1348+
if (isec->name.starts_with(".ctors") ||
1349+
isec->name.starts_with(".dtors"))
13511350
vec.push_back({isec, 65535 - get_ctor_dtor_priority(isec)});
13521351
else
13531352
vec.push_back({isec, get_init_fini_priority(isec)});
@@ -1555,13 +1554,13 @@ void fixup_ctors_in_init_array(Context<E> &ctx) {
15551554
if (Chunk<E> *chunk = find_chunk(ctx, ".init_array"))
15561555
if (OutputSection<E> *osec = chunk->to_osec())
15571556
for (InputSection<E> *isec : osec->members)
1558-
if (isec->name().starts_with(".ctors"))
1557+
if (isec->name.starts_with(".ctors"))
15591558
reverse_contents(*isec);
15601559

15611560
if (Chunk<E> *chunk = find_chunk(ctx, ".fini_array"))
15621561
if (OutputSection<E> *osec = chunk->to_osec())
15631562
for (InputSection<E> *isec : osec->members)
1564-
if (isec->name().starts_with(".dtors"))
1563+
if (isec->name.starts_with(".dtors"))
15651564
reverse_contents(*isec);
15661565
}
15671566

0 commit comments

Comments
 (0)