[lld] 648157b - [ELF] Move getOutputSectionName from Writer.cpp to LinkerScript.cpp. NFC
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 20 22:18:15 PST 2021
Author: Fangrui Song
Date: 2021-11-20T22:18:09-08:00
New Revision: 648157b05a1d5d17e03515e4858ccc70155f8c4e
URL: https://github.com/llvm/llvm-project/commit/648157b05a1d5d17e03515e4858ccc70155f8c4e
DIFF: https://github.com/llvm/llvm-project/commit/648157b05a1d5d17e03515e4858ccc70155f8c4e.diff
LOG: [ELF] Move getOutputSectionName from Writer.cpp to LinkerScript.cpp. NFC
and internalize it.
Added:
Modified:
lld/ELF/LinkerScript.cpp
lld/ELF/Writer.cpp
lld/ELF/Writer.h
Removed:
################################################################################
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index f332b03d757df..6657f21af6758 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -49,12 +49,73 @@ using namespace lld::elf;
LinkerScript *elf::script;
+static bool isSectionPrefix(StringRef prefix, StringRef name) {
+ return name.startswith(prefix) || name == prefix.drop_back();
+}
+
static uint64_t getOutputSectionVA(SectionBase *sec) {
OutputSection *os = sec->getOutputSection();
assert(os && "input section has no output section assigned");
return os ? os->addr : 0;
}
+static StringRef getOutputSectionName(const InputSectionBase *s) {
+ if (config->relocatable)
+ return s->name;
+
+ // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
+ // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
+ // technically required, but not doing it is odd). This code guarantees that.
+ if (auto *isec = dyn_cast<InputSection>(s)) {
+ if (InputSectionBase *rel = isec->getRelocatedSection()) {
+ OutputSection *out = rel->getOutputSection();
+ if (s->type == SHT_RELA)
+ return saver.save(".rela" + out->name);
+ return saver.save(".rel" + out->name);
+ }
+ }
+
+ // A BssSection created for a common symbol is identified as "COMMON" in
+ // linker scripts. It should go to .bss section.
+ if (s->name == "COMMON")
+ return ".bss";
+
+ if (script->hasSectionsCommand)
+ return s->name;
+
+ // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
+ // by grouping sections with certain prefixes.
+
+ // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
+ // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
+ // We provide an option -z keep-text-section-prefix to group such sections
+ // into separate output sections. This is more flexible. See also
+ // sortISDBySectionOrder().
+ // ".text.unknown" means the hotness of the section is unknown. When
+ // SampleFDO is used, if a function doesn't have sample, it could be very
+ // cold or it could be a new function never being sampled. Those functions
+ // will be kept in the ".text.unknown" section.
+ // ".text.split." holds symbols which are split out from functions in other
+ // input sections. For example, with -fsplit-machine-functions, placing the
+ // cold parts in .text.split instead of .text.unlikely mitigates against poor
+ // profile inaccuracy. Techniques such as hugepage remapping can make
+ // conservative decisions at the section granularity.
+ if (config->zKeepTextSectionPrefix)
+ for (StringRef v : {".text.hot.", ".text.unknown.", ".text.unlikely.",
+ ".text.startup.", ".text.exit.", ".text.split."})
+ if (isSectionPrefix(v, s->name))
+ return v.drop_back();
+
+ for (StringRef v :
+ {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
+ ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
+ ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."})
+ if (isSectionPrefix(v, s->name))
+ return v.drop_back();
+
+ return s->name;
+}
+
uint64_t ExprValue::getValue() const {
if (sec)
return alignTo(sec->getOffset(val) + getOutputSectionVA(sec),
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 6d97852aec434..1137825a4322c 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -91,67 +91,6 @@ template <class ELFT> class Writer {
};
} // anonymous namespace
-static bool isSectionPrefix(StringRef prefix, StringRef name) {
- return name.startswith(prefix) || name == prefix.drop_back();
-}
-
-StringRef elf::getOutputSectionName(const InputSectionBase *s) {
- if (config->relocatable)
- return s->name;
-
- // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
- // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
- // technically required, but not doing it is odd). This code guarantees that.
- if (auto *isec = dyn_cast<InputSection>(s)) {
- if (InputSectionBase *rel = isec->getRelocatedSection()) {
- OutputSection *out = rel->getOutputSection();
- if (s->type == SHT_RELA)
- return saver.save(".rela" + out->name);
- return saver.save(".rel" + out->name);
- }
- }
-
- // A BssSection created for a common symbol is identified as "COMMON" in
- // linker scripts. It should go to .bss section.
- if (s->name == "COMMON")
- return ".bss";
-
- if (script->hasSectionsCommand)
- return s->name;
-
- // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
- // by grouping sections with certain prefixes.
-
- // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
- // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
- // We provide an option -z keep-text-section-prefix to group such sections
- // into separate output sections. This is more flexible. See also
- // sortISDBySectionOrder().
- // ".text.unknown" means the hotness of the section is unknown. When
- // SampleFDO is used, if a function doesn't have sample, it could be very
- // cold or it could be a new function never being sampled. Those functions
- // will be kept in the ".text.unknown" section.
- // ".text.split." holds symbols which are split out from functions in other
- // input sections. For example, with -fsplit-machine-functions, placing the
- // cold parts in .text.split instead of .text.unlikely mitigates against poor
- // profile inaccuracy. Techniques such as hugepage remapping can make
- // conservative decisions at the section granularity.
- if (config->zKeepTextSectionPrefix)
- for (StringRef v : {".text.hot.", ".text.unknown.", ".text.unlikely.",
- ".text.startup.", ".text.exit.", ".text.split."})
- if (isSectionPrefix(v, s->name))
- return v.drop_back();
-
- for (StringRef v :
- {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
- ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
- ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."})
- if (isSectionPrefix(v, s->name))
- return v.drop_back();
-
- return s->name;
-}
-
static bool needsInterpSection() {
return !config->relocatable && !config->shared &&
!config->dynamicLinker.empty() && script->needsInterpSection();
diff --git a/lld/ELF/Writer.h b/lld/ELF/Writer.h
index 3698544d977be..9c4a5b98451db 100644
--- a/lld/ELF/Writer.h
+++ b/lld/ELF/Writer.h
@@ -51,7 +51,6 @@ struct PhdrEntry {
};
void addReservedSymbols();
-llvm::StringRef getOutputSectionName(const InputSectionBase *s);
template <class ELFT> uint32_t calcMipsEFlags();
More information about the llvm-commits
mailing list