[llvm-branch-commits] [llvm] [SHT_LLVM_FUNC_MAP][llvm-readobj]Introduce function address map section and emit dynamic instruction count(readobj part) (PR #124333)

James Henderson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Feb 19 01:14:14 PST 2025


================
@@ -940,6 +940,92 @@ ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec,
   return std::move(AddrMapsOrErr);
 }
 
+template <class ELFT>
+Expected<std::vector<FuncMap>>
+ELFFile<ELFT>::decodeFuncMap(const Elf_Shdr &Sec,
+                             const Elf_Shdr *RelaSec) const {
+  bool IsRelocatable = this->getHeader().e_type == ELF::ET_REL;
+
+  // This DenseMap maps the offset of each function (the location of the
+  // reference to the function in the SHT_LLVM_FUNC_MAP section) to the
+  // addend (the location of the function in the text section).
+  llvm::DenseMap<uint64_t, uint64_t> FunctionOffsetTranslations;
+  if (IsRelocatable && RelaSec) {
+    assert(RelaSec &&
+           "Can't read a SHT_LLVM_FUNC_ADDR_MAP section in a relocatable "
+           "object file without providing a relocation section.");
+    Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas =
+        this->relas(*RelaSec);
+    if (!Relas)
+      return createError("unable to read relocations for section " +
+                         describe(*this, Sec) + ": " +
+                         toString(Relas.takeError()));
+    for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
+      FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
----------------
jh7370 wrote:

Not all relocations function in the same way. Naively assuming that the `r_addend` and `r_offset` work like this is not going to be correct in some cases. The ELF gABI only describes `r_addend` as a "constant addend used to compute the value".

Have you looked into the Object/RelocationResolver.h? It's used elsewhere by llvm-readobj to calculate the values of relocations and may be of some use (see `printRelocatableStackSizes` for an example usage).

https://github.com/llvm/llvm-project/pull/124333


More information about the llvm-branch-commits mailing list