From 681cfeb59119f2ddc5418e4e3a48c632dcd1aa3e Mon Sep 17 00:00:00 2001 From: Jez Ng Date: Fri, 11 Jun 2021 19:49:53 -0400 Subject: [PATCH] [lld-macho][nfc] Have InputSection ctors take some parameters This is motivated by an upcoming diff in which the WordLiteralInputSection ctor sets itself up based on the value of its section flags. As such, it needs to be passed the `flags` value as part of its ctor parameters, instead of having them assigned after the fact in `parseSection()`. While refactoring code to make that possible, I figured it would make sense for the other InputSections to also take their initial values as ctor parameters. Reviewed By: #lld-macho, thakis Differential Revision: https://reviews.llvm.org/D103978 --- lld/MachO/ConcatOutputSection.cpp | 4 +--- lld/MachO/Driver.cpp | 5 ++-- lld/MachO/InputFiles.cpp | 48 +++++++++++++++++---------------------- lld/MachO/InputSection.cpp | 7 ++++++ lld/MachO/InputSection.h | 25 ++++++++++++++++---- lld/MachO/SyntheticSections.cpp | 9 +++----- 6 files changed, 55 insertions(+), 43 deletions(-) diff --git a/lld/MachO/ConcatOutputSection.cpp b/lld/MachO/ConcatOutputSection.cpp index 3b5a31d..dc12722 100644 --- a/lld/MachO/ConcatOutputSection.cpp +++ b/lld/MachO/ConcatOutputSection.cpp @@ -290,9 +290,7 @@ void ConcatOutputSection::finalize() { // unfinalized inputs[finalIdx]. fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun"); } - thunkInfo.isec = make(); - thunkInfo.isec->name = isec->name; - thunkInfo.isec->segname = isec->segname; + thunkInfo.isec = make(isec->segname, isec->name); thunkInfo.isec->parent = this; StringRef thunkName = saver.save(funcSym->getName() + ".thunk." + std::to_string(thunkInfo.sequence++)); diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp index 23c297b..4fc2693 100644 --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -533,10 +533,9 @@ static void replaceCommonSymbols() { if (common == nullptr) continue; - auto *isec = make(); + auto *isec = + make(segment_names::data, section_names::common); isec->file = common->getFile(); - isec->name = section_names::common; - isec->segname = segment_names::data; isec->align = common->align; // Casting to size_t will truncate large values on 32-bit architectures, // but it's not really worth supporting the linking of 64-bit programs on diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp index 4d93878..0b51292 100644 --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -242,29 +242,24 @@ InputFile::InputFile(Kind kind, const InterfaceFile &interface) : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {} template -static void parseSection(ObjFile *file, const uint8_t *buf, const Section &sec, - InputSection *isec) { - isec->file = file; - isec->name = - StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname))); - isec->segname = - StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname))); - isec->data = {isZeroFill(sec.flags) ? nullptr : buf + sec.offset, - static_cast(sec.size)}; - if (sec.align >= 32) - error("alignment " + std::to_string(sec.align) + " of section " + - isec->name + " is too large"); - else - isec->align = 1 << sec.align; - isec->flags = sec.flags; -} - -template void ObjFile::parseSections(ArrayRef
sections) { subsections.reserve(sections.size()); auto *buf = reinterpret_cast(mb.getBufferStart()); for (const Section &sec : sections) { + StringRef name = + StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname))); + StringRef segname = + StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname))); + ArrayRef data = {isZeroFill(sec.flags) ? nullptr + : buf + sec.offset, + static_cast(sec.size)}; + if (sec.align >= 32) + error("alignment " + std::to_string(sec.align) + " of section " + name + + " is too large"); + uint32_t align = 1 << sec.align; + uint32_t flags = sec.flags; + if (config->dedupLiterals && (sectionType(sec.flags) == S_CSTRING_LITERALS || isWordLiteralSection(sec.flags))) { @@ -276,18 +271,18 @@ void ObjFile::parseSections(ArrayRef
sections) { InputSection *isec; if (sectionType(sec.flags) == S_CSTRING_LITERALS) { - isec = make(); - parseSection(this, buf, sec, isec); + isec = + make(segname, name, this, data, align, flags); // FIXME: parallelize this? cast(isec)->splitIntoPieces(); } else { - isec = make(); - parseSection(this, buf, sec, isec); + isec = make(segname, name, this, data, align, + flags); } subsections.push_back({{0, isec}}); } else { - auto *isec = make(); - parseSection(this, buf, sec, isec); + auto *isec = + make(segname, name, this, data, align, flags); if (!(isDebugSection(isec->flags) && isec->segname == segment_names::dwarf)) { subsections.push_back({{0, isec}}); @@ -667,10 +662,9 @@ void ObjFile::parseSymbols(ArrayRef sectionHeaders, OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName) : InputFile(OpaqueKind, mb) { - ConcatInputSection *isec = make(); + ConcatInputSection *isec = + make(segName.take_front(16), sectName.take_front(16)); isec->file = this; - isec->name = sectName.take_front(16); - isec->segname = segName.take_front(16); const auto *buf = reinterpret_cast(mb.getBufferStart()); isec->data = {buf, mb.getBufferSize()}; isec->live = true; diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp index 29e5045..42f4a98 100644 --- a/lld/MachO/InputSection.cpp +++ b/lld/MachO/InputSection.cpp @@ -127,6 +127,13 @@ uint64_t CStringInputSection::getOffset(uint64_t off) const { return piece.outSecOff + addend; } +WordLiteralInputSection::WordLiteralInputSection(StringRef segname, + StringRef name, + InputFile *file, + ArrayRef data, + uint32_t align, uint32_t flags) + : InputSection(WordLiteralKind, segname, name, file, data, align, flags) {} + uint64_t WordLiteralInputSection::getFileOffset(uint64_t off) const { return parent->fileOff + getOffset(off); } diff --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h index bd9a232..44ee1f9 100644 --- a/lld/MachO/InputSection.h +++ b/lld/MachO/InputSection.h @@ -62,7 +62,13 @@ public: std::vector relocs; protected: - explicit InputSection(Kind kind) : sectionKind(kind) {} + InputSection(Kind kind, StringRef segname, StringRef name) + : name(name), segname(segname), sectionKind(kind) {} + + InputSection(Kind kind, StringRef segname, StringRef name, InputFile *file, + ArrayRef data, uint32_t align, uint32_t flags) + : file(file), name(name), segname(segname), align(align), flags(flags), + data(data), sectionKind(kind) {} private: Kind sectionKind; @@ -73,7 +79,13 @@ private: // contents merged before output. class ConcatInputSection : public InputSection { public: - ConcatInputSection() : InputSection(ConcatKind) {} + ConcatInputSection(StringRef segname, StringRef name) + : InputSection(ConcatKind, segname, name) {} + + ConcatInputSection(StringRef segname, StringRef name, InputFile *file, + ArrayRef data, uint32_t align, uint32_t flags) + : InputSection(ConcatKind, segname, name, file, data, align, flags) {} + uint64_t getFileOffset(uint64_t off) const override; uint64_t getOffset(uint64_t off) const override { return outSecOff + off; } uint64_t getVA() const { return InputSection::getVA(0); } @@ -123,7 +135,10 @@ struct StringPiece { // conservative behavior we can certainly implement that. class CStringInputSection : public InputSection { public: - CStringInputSection() : InputSection(CStringLiteralKind) {} + CStringInputSection(StringRef segname, StringRef name, InputFile *file, + ArrayRef data, uint32_t align, uint32_t flags) + : InputSection(CStringLiteralKind, segname, name, file, data, align, + flags) {} uint64_t getFileOffset(uint64_t off) const override; uint64_t getOffset(uint64_t off) const override; // FIXME implement this @@ -152,7 +167,9 @@ public: class WordLiteralInputSection : public InputSection { public: - WordLiteralInputSection() : InputSection(WordLiteralKind) {} + WordLiteralInputSection(StringRef segname, StringRef name, InputFile *file, + ArrayRef data, uint32_t align, + uint32_t flags); uint64_t getFileOffset(uint64_t off) const override; uint64_t getOffset(uint64_t off) const override; // FIXME implement this diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp index 570a2da..9183f18 100644 --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -48,9 +48,7 @@ std::vector macho::syntheticSections; SyntheticSection::SyntheticSection(const char *segname, const char *name) : OutputSection(SyntheticKind, name), segname(segname) { - isec = make(); - isec->segname = segname; - isec->name = name; + isec = make(segname, name); isec->parent = this; syntheticSections.push_back(this); } @@ -479,9 +477,8 @@ void StubHelperSection::setup() { /*noDeadStrip=*/false); } -ImageLoaderCacheSection::ImageLoaderCacheSection() { - segname = segment_names::data; - name = section_names::data; +ImageLoaderCacheSection::ImageLoaderCacheSection() + : ConcatInputSection(segment_names::data, section_names::data) { uint8_t *arr = bAlloc.Allocate(target->wordSize); memset(arr, 0, target->wordSize); data = {arr, target->wordSize}; -- 2.7.4