[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
This commit is contained in:
Jez Ng
2021-06-11 19:49:53 -04:00
parent 7f2ba39b16
commit 681cfeb591
6 changed files with 55 additions and 43 deletions

View File

@@ -290,9 +290,7 @@ void ConcatOutputSection::finalize() {
// unfinalized inputs[finalIdx].
fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
}
thunkInfo.isec = make<ConcatInputSection>();
thunkInfo.isec->name = isec->name;
thunkInfo.isec->segname = isec->segname;
thunkInfo.isec = make<ConcatInputSection>(isec->segname, isec->name);
thunkInfo.isec->parent = this;
StringRef thunkName = saver.save(funcSym->getName() + ".thunk." +
std::to_string(thunkInfo.sequence++));

View File

@@ -533,10 +533,9 @@ static void replaceCommonSymbols() {
if (common == nullptr)
continue;
auto *isec = make<ConcatInputSection>();
auto *isec =
make<ConcatInputSection>(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

View File

@@ -241,30 +241,25 @@ Optional<MemoryBufferRef> macho::readFile(StringRef path) {
InputFile::InputFile(Kind kind, const InterfaceFile &interface)
: id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
template <class Section>
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<size_t>(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 <class Section>
void ObjFile::parseSections(ArrayRef<Section> sections) {
subsections.reserve(sections.size());
auto *buf = reinterpret_cast<const uint8_t *>(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<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
: buf + sec.offset,
static_cast<size_t>(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<Section> sections) {
InputSection *isec;
if (sectionType(sec.flags) == S_CSTRING_LITERALS) {
isec = make<CStringInputSection>();
parseSection(this, buf, sec, isec);
isec =
make<CStringInputSection>(segname, name, this, data, align, flags);
// FIXME: parallelize this?
cast<CStringInputSection>(isec)->splitIntoPieces();
} else {
isec = make<WordLiteralInputSection>();
parseSection(this, buf, sec, isec);
isec = make<WordLiteralInputSection>(segname, name, this, data, align,
flags);
}
subsections.push_back({{0, isec}});
} else {
auto *isec = make<ConcatInputSection>();
parseSection(this, buf, sec, isec);
auto *isec =
make<ConcatInputSection>(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<typename LP::section> sectionHeaders,
OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
StringRef sectName)
: InputFile(OpaqueKind, mb) {
ConcatInputSection *isec = make<ConcatInputSection>();
ConcatInputSection *isec =
make<ConcatInputSection>(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<const uint8_t *>(mb.getBufferStart());
isec->data = {buf, mb.getBufferSize()};
isec->live = true;

View File

@@ -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<uint8_t> 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);
}

View File

@@ -62,7 +62,13 @@ public:
std::vector<Reloc> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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

View File

@@ -48,9 +48,7 @@ std::vector<SyntheticSection *> macho::syntheticSections;
SyntheticSection::SyntheticSection(const char *segname, const char *name)
: OutputSection(SyntheticKind, name), segname(segname) {
isec = make<ConcatInputSection>();
isec->segname = segname;
isec->name = name;
isec = make<ConcatInputSection>(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<uint8_t>(target->wordSize);
memset(arr, 0, target->wordSize);
data = {arr, target->wordSize};