[lld] bf7f3dd - [ELF] Move outSecOff addition from InputSection::writeTo to the caller

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 26 12:11:47 PST 2021


Author: Fangrui Song
Date: 2021-12-26T12:11:41-08:00
New Revision: bf7f3dd74ee3d6c8a219ff5434df12451c9aad95

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

LOG: [ELF] Move outSecOff addition from InputSection::writeTo to the caller

Simplify the code a bit and improve consistency with SyntheticSection::writeTo.

Added: 
    

Modified: 
    lld/ELF/InputSection.cpp
    lld/ELF/OutputSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index e1ee3def89f36..6d26e19aac48f 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -1227,7 +1227,7 @@ void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
 
 template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
   if (auto *s = dyn_cast<SyntheticSection>(this)) {
-    s->writeTo(buf + outSecOff);
+    s->writeTo(buf);
     return;
   }
 
@@ -1236,17 +1236,17 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
   // If -r or --emit-relocs is given, then an InputSection
   // may be a relocation section.
   if (LLVM_UNLIKELY(type == SHT_RELA)) {
-    copyRelocations<ELFT>(buf + outSecOff, getDataAs<typename ELFT::Rela>());
+    copyRelocations<ELFT>(buf, getDataAs<typename ELFT::Rela>());
     return;
   }
   if (LLVM_UNLIKELY(type == SHT_REL)) {
-    copyRelocations<ELFT>(buf + outSecOff, getDataAs<typename ELFT::Rel>());
+    copyRelocations<ELFT>(buf, getDataAs<typename ELFT::Rel>());
     return;
   }
 
   // If -r is given, we may have a SHT_GROUP section.
   if (LLVM_UNLIKELY(type == SHT_GROUP)) {
-    copyShtGroup<ELFT>(buf + outSecOff);
+    copyShtGroup<ELFT>(buf);
     return;
   }
 
@@ -1254,20 +1254,19 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
   // to the buffer.
   if (uncompressedSize >= 0) {
     size_t size = uncompressedSize;
-    if (Error e = zlib::uncompress(toStringRef(rawData),
-                                   (char *)(buf + outSecOff), size))
+    if (Error e = zlib::uncompress(toStringRef(rawData), (char *)buf, size))
       fatal(toString(this) +
             ": uncompress failed: " + llvm::toString(std::move(e)));
-    uint8_t *bufEnd = buf + outSecOff + size;
-    relocate<ELFT>(buf + outSecOff, bufEnd);
+    uint8_t *bufEnd = buf + size;
+    relocate<ELFT>(buf, bufEnd);
     return;
   }
 
   // Copy section contents from source object file to output file
   // and then apply relocations.
-  memcpy(buf + outSecOff, data().data(), data().size());
-  uint8_t *bufEnd = buf + outSecOff + data().size();
-  relocate<ELFT>(buf + outSecOff, bufEnd);
+  memcpy(buf, data().data(), data().size());
+  uint8_t *bufEnd = buf + data().size();
+  relocate<ELFT>(buf, bufEnd);
 }
 
 void InputSection::replace(InputSection *other) {

diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 4a03ac387814a..cd53eecd8756a 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -355,7 +355,7 @@ template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
 
   parallelForEachN(0, sections.size(), [&](size_t i) {
     InputSection *isec = sections[i];
-    isec->writeTo<ELFT>(buf);
+    isec->writeTo<ELFT>(buf + isec->outSecOff);
 
     // Fill gaps between sections.
     if (nonZeroFiller) {


        


More information about the llvm-commits mailing list