[lld] 098430c - [lld-macho][nfc] Simplify LC_DATA_IN_CODE generation

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 10 22:02:10 PST 2021


Author: Jez Ng
Date: 2021-12-11T01:01:57-05:00
New Revision: 098430cd25e7d1d5035932db2e4e5f9fb72adfee

URL: https://github.com/llvm/llvm-project/commit/098430cd25e7d1d5035932db2e4e5f9fb72adfee
DIFF: https://github.com/llvm/llvm-project/commit/098430cd25e7d1d5035932db2e4e5f9fb72adfee.diff

LOG: [lld-macho][nfc] Simplify LC_DATA_IN_CODE generation

1. After D113241, we have the section address easily accessible and no
   longer need to iterate across the LC_SEGMENT commands to emit
   LC_DATA_IN_CODE.

2. There's no need to store a pointer to the data in code entries during
   the parse step; we can just look it up as part of the output step.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D115556

Added: 
    

Modified: 
    lld/MachO/InputFiles.cpp
    lld/MachO/InputFiles.h
    lld/MachO/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 23f28e63bf59e..d8e02732cf6e5 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -879,8 +879,6 @@ template <class LP> void ObjFile::parse() {
                        sections[i].subsections);
 
   parseDebugInfo();
-  if (config->emitDataInCodeInfo)
-    parseDataInCode();
   if (compactUnwindSection)
     registerCompactUnwind();
 }
@@ -908,19 +906,14 @@ void ObjFile::parseDebugInfo() {
   compileUnit = it->get();
 }
 
-void ObjFile::parseDataInCode() {
+ArrayRef<data_in_code_entry> ObjFile::getDataInCode() const {
   const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
   const load_command *cmd = findCommand(buf, LC_DATA_IN_CODE);
   if (!cmd)
-    return;
+    return {};
   const auto *c = reinterpret_cast<const linkedit_data_command *>(cmd);
-  dataInCodeEntries = {
-      reinterpret_cast<const data_in_code_entry *>(buf + c->dataoff),
-      c->datasize / sizeof(data_in_code_entry)};
-  assert(is_sorted(dataInCodeEntries, [](const data_in_code_entry &lhs,
-                                         const data_in_code_entry &rhs) {
-    return lhs.offset < rhs.offset;
-  }));
+  return {reinterpret_cast<const data_in_code_entry *>(buf + c->dataoff),
+          c->datasize / sizeof(data_in_code_entry)};
 }
 
 // Create pointers from symbols to their associated compact unwind entries.

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index 93794cb5a4aae..47e77cc2c796b 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -108,12 +108,13 @@ class InputFile {
 class ObjFile final : public InputFile {
 public:
   ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName);
+  ArrayRef<llvm::MachO::data_in_code_entry> getDataInCode() const;
+
   static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
 
   llvm::DWARFUnit *compileUnit = nullptr;
   const uint32_t modTime;
   std::vector<ConcatInputSection *> debugSections;
-  ArrayRef<llvm::MachO::data_in_code_entry> dataInCodeEntries;
 
 private:
   Section *compactUnwindSection = nullptr;
@@ -130,7 +131,6 @@ class ObjFile final : public InputFile {
   void parseRelocations(ArrayRef<SectionHeader> sectionHeaders,
                         const SectionHeader &, Subsections &);
   void parseDebugInfo();
-  void parseDataInCode();
   void registerCompactUnwind();
 };
 

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 99a15666c8fa1..b64a9db485c50 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -733,35 +733,30 @@ DataInCodeSection::DataInCodeSection()
 
 template <class LP>
 static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
-  using SegmentCommand = typename LP::segment_command;
-  using SectionHeader = typename LP::section;
-
   std::vector<MachO::data_in_code_entry> dataInCodeEntries;
   for (const InputFile *inputFile : inputFiles) {
     if (!isa<ObjFile>(inputFile))
       continue;
     const ObjFile *objFile = cast<ObjFile>(inputFile);
-    const auto *c = reinterpret_cast<const SegmentCommand *>(
-        findCommand(objFile->mb.getBufferStart(), LP::segmentLCType));
-    if (!c)
-      continue;
-    ArrayRef<SectionHeader> sectionHeaders{
-        reinterpret_cast<const SectionHeader *>(c + 1), c->nsects};
-
-    ArrayRef<MachO::data_in_code_entry> entries = objFile->dataInCodeEntries;
+    ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode();
     if (entries.empty())
       continue;
+
+    assert(is_sorted(dataInCodeEntries, [](const data_in_code_entry &lhs,
+                                           const data_in_code_entry &rhs) {
+      return lhs.offset < rhs.offset;
+    }));
     // For each code subsection find 'data in code' entries residing in it.
     // Compute the new offset values as
     // <offset within subsection> + <subsection address> - <__TEXT address>.
-    for (size_t i = 0, n = sectionHeaders.size(); i < n; ++i) {
-      for (const Subsection &subsec : objFile->sections[i].subsections) {
+    for (const Section &section : objFile->sections) {
+      for (const Subsection &subsec : section.subsections) {
         const InputSection *isec = subsec.isec;
         if (!isCodeSection(isec))
           continue;
         if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput())
           continue;
-        const uint64_t beginAddr = sectionHeaders[i].addr + subsec.offset;
+        const uint64_t beginAddr = section.address + subsec.offset;
         auto it = llvm::lower_bound(
             entries, beginAddr,
             [](const MachO::data_in_code_entry &entry, uint64_t addr) {


        


More information about the llvm-commits mailing list