[lld] 8565a87 - [ELF] Simplify MergeInputSection::getParentOffset. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 28 10:02:40 PDT 2022


Author: Fangrui Song
Date: 2022-03-28T10:02:35-07:00
New Revision: 8565a87fd4407dc8c1100fe73d232d7260b98f42

URL: https://github.com/llvm/llvm-project/commit/8565a87fd4407dc8c1100fe73d232d7260b98f42
DIFF: https://github.com/llvm/llvm-project/commit/8565a87fd4407dc8c1100fe73d232d7260b98f42.diff

LOG: [ELF] Simplify MergeInputSection::getParentOffset. NFC

and remove overly verbose comments.

Added: 
    

Modified: 
    lld/ELF/InputSection.cpp
    lld/ELF/InputSection.h
    lld/ELF/MarkLive.cpp
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 1d23c065000cb..d45ab9480f421 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -1410,26 +1410,17 @@ void MergeInputSection::splitIntoPieces() {
     splitNonStrings(data(), entsize);
 }
 
-SectionPiece *MergeInputSection::getSectionPiece(uint64_t offset) {
-  if (this->rawData.size() <= offset)
+SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) {
+  if (rawData.size() <= offset)
     fatal(toString(this) + ": offset is outside the section");
-
-  // If Offset is not at beginning of a section piece, it is not in the map.
-  // In that case we need to  do a binary search of the original section piece vector.
-  auto it = partition_point(
-      pieces, [=](SectionPiece p) { return p.inputOff <= offset; });
-  return &it[-1];
+  return partition_point(
+      pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];
 }
 
-// Returns the offset in an output section for a given input offset.
-// Because contents of a mergeable section is not contiguous in output,
-// it is not just an addition to a base output offset.
+// Return the offset in an output section for a given input offset.
 uint64_t MergeInputSection::getParentOffset(uint64_t offset) const {
-  // If Offset is not at beginning of a section piece, it is not in the map.
-  // In that case we need to search from the original section piece vector.
-  const SectionPiece &piece = *getSectionPiece(offset);
-  uint64_t addend = offset - piece.inputOff;
-  return piece.outputOff + addend;
+  const SectionPiece &piece = getSectionPiece(offset);
+  return piece.outputOff + (offset - piece.inputOff);
 }
 
 template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &,

diff  --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index 7e23e565b3d34..b99a344618ada 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -282,8 +282,8 @@ class MergeInputSection : public InputSectionBase {
   }
 
   // Returns the SectionPiece at a given input section offset.
-  SectionPiece *getSectionPiece(uint64_t offset);
-  const SectionPiece *getSectionPiece(uint64_t offset) const {
+  SectionPiece &getSectionPiece(uint64_t offset);
+  const SectionPiece &getSectionPiece(uint64_t offset) const {
     return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
   }
 

diff  --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index 7a97af96d3ed1..42875c10cb2b7 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -198,7 +198,7 @@ void MarkLive<ELFT>::enqueue(InputSectionBase *sec, uint64_t offset) {
   // (splittable) sections, each piece of data has independent liveness bit.
   // So we explicitly tell it which offset is in use.
   if (auto *ms = dyn_cast<MergeInputSection>(sec))
-    ms->getSectionPiece(offset)->live = true;
+    ms->getSectionPiece(offset).live = true;
 
   // Set Sec->Partition to the meet (i.e. the "minimum") of Partition and
   // Sec->Partition in the following lattice: 1 < other < 0. If Sec->Partition

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index fa374fd08e432..8ca72b3455927 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -673,7 +673,7 @@ static bool includeInSymtab(const Symbol &b) {
       return true;
 
     if (auto *s = dyn_cast<MergeInputSection>(sec))
-      return s->getSectionPiece(d->value)->live;
+      return s->getSectionPiece(d->value).live;
     return sec->isLive();
   }
   return b.used || !config->gcSections;


        


More information about the llvm-commits mailing list