[llvm] r351417 - [llvm-objdump] - Simplify the getRelocationValueString. NFCI.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 17 01:13:18 PST 2019


Author: grimar
Date: Thu Jan 17 01:13:17 2019
New Revision: 351417

URL: http://llvm.org/viewvc/llvm-project?rev=351417&view=rev
Log:
[llvm-objdump] - Simplify the getRelocationValueString. NFCI.

This refactors the getRelocationValueString method.
It is a bit overcomplicated and it is possible to reduce it without
losing the functionality it seems.

Differential revision: https://reviews.llvm.org/D56778

Modified:
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=351417&r1=351416&r2=351417&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Thu Jan 17 01:13:17 2019
@@ -491,44 +491,32 @@ static std::error_code getRelocationValu
   auto SecOrErr = EF.getSection(Rel.d.a);
   if (!SecOrErr)
     return errorToErrorCode(SecOrErr.takeError());
-  const Elf_Shdr *Sec = *SecOrErr;
-  auto SymTabOrErr = EF.getSection(Sec->sh_link);
-  if (!SymTabOrErr)
-    return errorToErrorCode(SymTabOrErr.takeError());
-  const Elf_Shdr *SymTab = *SymTabOrErr;
-  assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
-         SymTab->sh_type == ELF::SHT_DYNSYM);
-  auto StrTabSec = EF.getSection(SymTab->sh_link);
-  if (!StrTabSec)
-    return errorToErrorCode(StrTabSec.takeError());
-  auto StrTabOrErr = EF.getStringTable(*StrTabSec);
-  if (!StrTabOrErr)
-    return errorToErrorCode(StrTabOrErr.takeError());
-  StringRef StrTab = *StrTabOrErr;
+
   int64_t Addend = 0;
   // If there is no Symbol associated with the relocation, we set the undef
   // boolean value to 'true'. This will prevent us from calling functions that
   // requires the relocation to be associated with a symbol.
+  //
+  // In SHT_REL case we would need to read the addend from section data.
+  // GNU objdump does not do that and we just follow for simplicity atm.
   bool Undef = false;
-  switch (Sec->sh_type) {
-  default:
-    return object_error::parse_failed;
-  case ELF::SHT_REL: {
-    // TODO: Read implicit addend from section data.
-    break;
-  }
-  case ELF::SHT_RELA: {
+  if ((*SecOrErr)->sh_type == ELF::SHT_RELA) {
     const Elf_Rela *ERela = Obj->getRela(Rel);
     Addend = ERela->r_addend;
     Undef = ERela->getSymbol(false) == 0;
-    break;
-  }
+  } else if ((*SecOrErr)->sh_type != ELF::SHT_REL) {
+    return object_error::parse_failed;
   }
-  std::string Target;
+
+  // Default scheme is to print Target, as well as "+ <addend>" for nonzero
+  // addend. Should be acceptable for all normal purposes.
+  std::string FmtBuf;
+  raw_string_ostream Fmt(FmtBuf);
+
   if (!Undef) {
     symbol_iterator SI = RelRef.getSymbol();
-    const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
-    if (symb->getType() == ELF::STT_SECTION) {
+    const Elf_Sym *Sym = Obj->getSymbol(SI->getRawDataRefImpl());
+    if (Sym->getType() == ELF::STT_SECTION) {
       Expected<section_iterator> SymSI = SI->getSection();
       if (!SymSI)
         return errorToErrorCode(SymSI.takeError());
@@ -536,24 +524,20 @@ static std::error_code getRelocationValu
       auto SecName = EF.getSectionName(SymSec);
       if (!SecName)
         return errorToErrorCode(SecName.takeError());
-      Target = *SecName;
+      Fmt << *SecName;
     } else {
-      Expected<StringRef> SymName = symb->getName(StrTab);
+      Expected<StringRef> SymName = SI->getName();
       if (!SymName)
         return errorToErrorCode(SymName.takeError());
       if (Demangle)
-        Target = demangle(*SymName);
+        Fmt << demangle(*SymName);
       else
-        Target = *SymName;
+        Fmt << *SymName;
     }
-  } else
-    Target = "*ABS*";
+  } else {
+    Fmt << "*ABS*";
+  }
 
-  // Default scheme is to print Target, as well as "+ <addend>" for nonzero
-  // addend. Should be acceptable for all normal purposes.
-  std::string FmtBuf;
-  raw_string_ostream Fmt(FmtBuf);
-  Fmt << Target;
   if (Addend != 0)
     Fmt << (Addend < 0 ? "" : "+") << Addend;
   Fmt.flush();




More information about the llvm-commits mailing list