[llvm-commits] [PATCH] Add a funciton to get the segment name of a section.

Michael Spencer bigcheesegs at gmail.com
Wed Dec 12 11:32:58 PST 2012


On Wed, Dec 12, 2012 at 6:36 AM, Rafael Ávila de Espíndola
<rafael.espindola at gmail.com> wrote:
> On MachO, sections also have segment names. When a tool looking at a .o file
> prints a segment name, this is what they mean. In reality, a .o has only one,
> anonymous, segment.
>
> This patch adds a MachO only function to fetch that segment name. I named it
> getSectionFinalSegmentName since the main use for the name seems to be informing
> the linker with segment this section should go to.
>
> The patch also changes MachOObjectFile::getSectionName to return just the
> section name instead of computing SegmentName,SectionName.
> ---
>  include/llvm/Object/MachO.h         |  5 +++++
>  lib/Object/MachOObjectFile.cpp      | 35 +++++++++++++++++++++++++----------
>  tools/llvm-objdump/MachODump.cpp    |  8 +++++++-
>  tools/llvm-objdump/llvm-objdump.cpp | 14 +++++++++++---
>  4 files changed, 48 insertions(+), 14 deletions(-)
>
> diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h
> index da972a2..ed7aabd 100644
> --- a/include/llvm/Object/MachO.h
> +++ b/include/llvm/Object/MachO.h
> @@ -44,6 +44,11 @@ public:
>    virtual unsigned getArch() const;
>    virtual StringRef getLoadName() const;
>
> +  // In a MachO file, sections have a segment name. This is used in the .o
> +  // files. They have a single segment, but this field specifies which segment
> +  // a section should be put in in the final object.
> +  error_code getSectionFinalSegmentName(DataRefImpl Sec, StringRef &Res) const;
> +
>    MachOObject *getObject() { return MachOObj.get(); }
>
>    static inline bool classof(const Binary *v) {
> diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp
> index a38fac7..392563a 100644
> --- a/lib/Object/MachOObjectFile.cpp
> +++ b/lib/Object/MachOObjectFile.cpp
> @@ -473,28 +473,43 @@ static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
>    return false;
>  }
>
> +static StringRef parseSegmentOrSectionName(const char *P) {
> +  if (P[15] == 0)
> +    // Null terminated.
> +    return P;
> +  // Not null terminated, so this is a 16 char string.
> +  return StringRef(P, 16);
> +}
> +
>  error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
>                                             StringRef &Result) const {
> -  // FIXME: thread safety.
> -  static char result[34];
>    if (is64BitLoadCommand(MachOObj.get(), DRI)) {
>      LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
>      InMemoryStruct<macho::Section64> Sect;
>      MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
> -
> -    strcpy(result, Sect->SegmentName);
> -    strcat(result, ",");
> -    strcat(result, Sect->Name);
> +    Result = parseSegmentOrSectionName(Sect->Name);
>    } else {
>      LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
>      InMemoryStruct<macho::Section> Sect;
>      MachOObj->ReadSection(LCI, DRI.d.b, Sect);
> +    Result = parseSegmentOrSectionName(Sect->Name);
> +  }
> +  return object_error::success;
> +}
>
> -    strcpy(result, Sect->SegmentName);
> -    strcat(result, ",");
> -    strcat(result, Sect->Name);
> +error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
> +                                                       StringRef &Res) const {
> +  if (is64BitLoadCommand(MachOObj.get(), Sec)) {
> +    LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
> +    InMemoryStruct<macho::Section64> Sect;
> +    MachOObj->ReadSection64(LCI, Sec.d.b, Sect);
> +    Res = parseSegmentOrSectionName(Sect->SegmentName);
> +  } else {
> +    LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
> +    InMemoryStruct<macho::Section> Sect;
> +    MachOObj->ReadSection(LCI, Sec.d.b, Sect);
> +    Res = parseSegmentOrSectionName(Sect->SegmentName);
>    }
> -  Result = StringRef(result);
>    return object_error::success;
>  }
>
> diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp
> index 3a35038..c324ff1 100644
> --- a/tools/llvm-objdump/MachODump.cpp
> +++ b/tools/llvm-objdump/MachODump.cpp
> @@ -334,9 +334,15 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
>    for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
>      StringRef SectName;
>      if (Sections[SectIdx].getName(SectName) ||
> -        SectName.compare("__TEXT,__text"))
> +        SectName != "__text")
>        continue; // Skip non-text sections
>
> +    StringRef SegmentName;
> +    DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
> +    if (MachOOF->getSectionFinalSegmentName(DR, SegmentName) ||
> +        SegmentName != "__TEXT")
> +      continue;
> +
>      // Insert the functions from the function starts segment into our map.
>      uint64_t VMAddr;
>      Sections[SectIdx].getAddress(VMAddr);
> diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp
> index 2838a2a..9edcae8 100644
> --- a/tools/llvm-objdump/llvm-objdump.cpp
> +++ b/tools/llvm-objdump/llvm-objdump.cpp
> @@ -28,6 +28,7 @@
>  #include "llvm/MC/MCSubtargetInfo.h"
>  #include "llvm/Object/Archive.h"
>  #include "llvm/Object/COFF.h"
> +#include "llvm/Object/MachO.h"
>  #include "llvm/Object/ObjectFile.h"
>  #include "llvm/Support/Casting.h"
>  #include "llvm/Support/CommandLine.h"
> @@ -72,9 +73,9 @@ static cl::opt<bool>
>  SymbolTable("t", cl::desc("Display the symbol table"));
>
>  static cl::opt<bool>
> -MachO("macho", cl::desc("Use MachO specific object file parser"));
> +MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
>  static cl::alias
> -MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
> +MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
>
>  cl::opt<std::string>
>  llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
> @@ -567,6 +568,13 @@ static void PrintSymbolTable(const ObjectFile *o) {
>        else if (Section == o->end_sections())
>          outs() << "*UND*";
>        else {
> +        if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(o)) {
> +          StringRef SegmentName;
> +          DataRefImpl DR = Section->getRawDataRefImpl();
> +          if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
> +            SegmentName = "";
> +          outs() << SegmentName << ",";
> +        }
>          StringRef SectionName;
>          if (error(Section->getName(SectionName)))
>            SectionName = "";
> @@ -640,7 +648,7 @@ static void DumpInput(StringRef file) {
>      return;
>    }
>
> -  if (MachO && Disassemble) {
> +  if (MachOOpt && Disassemble) {
>      DisassembleInputMachO(file);
>      return;
>    }
> --
> 1.7.11.7
>

LGTM

- Michael Spencer




More information about the llvm-commits mailing list