[PATCH] D85086: [ELF] --oformat=binary: use LMA to compute file offsets

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 02:07:53 PDT 2020


grimar added a comment.

OK. I have no more objections about this then. Happy if others are happy.



================
Comment at: lld/ELF/Writer.cpp:2564
+      fileSize = std::max(fileSize, sec->offset + sec->size);
+    }
 }
----------------
Consider setting the `offset` field just once and deduplicating the
 `sec->type != SHT_NOBITS && (sec->flags & SHF_ALLOC) && sec->size > 0` condition:

E.g.
```
  auto needsOffset = [](OutputSection &sec) {
    return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
  };

  uint64_t minAddr = UINT64_MAX;
  for (OutputSection *sec : outputSections)
    if (needsOffset(*sec))
      minAddr = std::min(minAddr, sec->getLMA());

  fileSize = 0;
  for (OutputSection *sec : outputSections)
    if (needsOffset(*sec)) {
      sec->offset = sec->getLMA() - minAddr;
      fileSize = std::max(fileSize, sec->offset + sec->size);
    }
```

Or

```
  uint64_t minAddr = UINT64_MAX;
  std::vector<OutputSection *> sections;
  for (OutputSection *sec : outputSections) {
    if (sec.type = SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0) {
      minAddr = std::min(minAddr, sec->getLMA());
      sections.push_back(sec);
    }
  }

  fileSize = 0;
  for (OutputSection *sec : sections) {
    sec->offset = sec->getLMA() - minAddr;
    fileSize = std::max(fileSize, sec->offset + sec->size);
  }
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85086/new/

https://reviews.llvm.org/D85086



More information about the llvm-commits mailing list