[llvm-commits] ObjectFile SectionRef functions patch

Danil Malyshev dmalyshev at accesssoftek.com
Mon Feb 27 15:12:00 PST 2012


Hello Michael,

> Nobits is an ELF specific term. isSectionBSS already handles nearly
> this case. What semantics are you looking for?

I looking for sections who not allocated in the object file but need allocate in memory. Its can be not only BSS section. 

> As for isExecutable, I believe it's time to convert all the isX
> functions over to a getFlags function that gets them all.

I think is the best way. But at first we need make a list of flags satisfying of all.
For example, I do not quite understand the isText() - for ELF is the SHF_EXECINSTR flag, so this is what I need, but for MachO it's only section named "__ text".


Regards,
Danil


-----Original Message-----
From: Michael Spencer [mailto:bigcheesegs at gmail.com] 
Sent: Tuesday, February 28, 2012 1:55 AM
To: Danil Malyshev
Cc: llvm-commits at cs.uiuc.edu
Subject: Re: [llvm-commits] ObjectFile SectionRef functions patch

On Mon, Feb 27, 2012 at 10:32 AM, Danil Malyshev
<dmalyshev at accesssoftek.com> wrote:
> Hello everyone,
>
> Please find attached the patch for review.
>
> The patch fixes MachOObjectFile::isSectionData() and
> MachOObjectFile::isSectionBSS() and adds two methods SectionRef::isNobits()
> and SectionRef::isExecutable().
>
> Regards,
>
> Danil

Nobits is an ELF specific term. isSectionBSS already handles nearly
this case. What semantics are you looking for?

As for isExecutable, I believe it's time to convert all the isX
functions over to a getFlags function that gets them all.

Index: include/llvm/Object/COFF.h
===================================================================
--- include/llvm/Object/COFF.h	(revision 151528)
+++ include/llvm/Object/COFF.h	(working copy)
@@ -126,6 +126,8 @@
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
+  virtual error_code isSectionNobits(DataRefImpl Sec, bool &Res) const;
+  virtual error_code isSectionExecutable(DataRefImpl Sec, bool &Res) const;
   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
                                            bool &Result) const;
   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
Index: include/llvm/Object/ELF.h
===================================================================
--- include/llvm/Object/ELF.h	(revision 151528)
+++ include/llvm/Object/ELF.h	(working copy)
@@ -349,6 +349,8 @@
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
+  virtual error_code isSectionNobits(DataRefImpl Sec, bool &Res) const;
+  virtual error_code isSectionExecutable(DataRefImpl Sec, bool &Res) const;
   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
                                            bool &Result) const;
   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
@@ -801,6 +803,24 @@

 template<support::endianness target_endianness, bool is64Bits>
 error_code ELFObjectFile<target_endianness, is64Bits>
+                        ::isSectionNobits(DataRefImpl Sec,
+                                          bool &Result) const {
+  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
+  Result = (sec->sh_type == ELF::SHT_NOBITS);
+  return object_error::success;
+}
+
+template<support::endianness target_endianness, bool is64Bits>
+error_code ELFObjectFile<target_endianness, is64Bits>
+                        ::isSectionExecutable(DataRefImpl Sec,
+                                              bool &Result) const {
+  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
+  Result = (sec->sh_flags & ELF::SHF_EXECINSTR);
+  return object_error::success;
+}
+
+template<support::endianness target_endianness, bool is64Bits>
+error_code ELFObjectFile<target_endianness, is64Bits>
                           ::sectionContainsSymbol(DataRefImpl Sec,
                                                   DataRefImpl Symb,
                                                   bool &Result) const {
Index: include/llvm/Object/MachO.h
===================================================================
--- include/llvm/Object/MachO.h	(revision 151528)
+++ include/llvm/Object/MachO.h	(working copy)
@@ -70,6 +70,8 @@
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
+  virtual error_code isSectionNobits(DataRefImpl Sec, bool &Res) const;
+  virtual error_code isSectionExecutable(DataRefImpl Sec, bool &Res) const;
   virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
                                            bool &Result) const;
   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
Index: include/llvm/Object/ObjectFile.h
===================================================================
--- include/llvm/Object/ObjectFile.h	(revision 151528)
+++ include/llvm/Object/ObjectFile.h	(working copy)
@@ -159,6 +159,8 @@
   error_code isText(bool &Result) const;
   error_code isData(bool &Result) const;
   error_code isBSS(bool &Result) const;
+  error_code isNobits(bool &Result) const;
+  error_code isExecutable(bool &Result) const;

   error_code containsSymbol(SymbolRef S, bool &Result) const;

@@ -279,6 +281,8 @@
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
+  virtual error_code isSectionNobits(DataRefImpl Sec, bool &Res) const = 0;
+  virtual error_code isSectionExecutable(DataRefImpl Sec, bool &Res) const = 0;
   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
                                            bool &Result) const = 0;
   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
@@ -458,6 +462,14 @@
   return OwningObject->isSectionBSS(SectionPimpl, Result);
 }

+inline error_code SectionRef::isNobits(bool &Result) const {
+  return OwningObject->isSectionNobits(SectionPimpl, Result);
+}
+
+inline error_code SectionRef::isExecutable(bool &Result) const {
+  return OwningObject->isSectionExecutable(SectionPimpl, Result);
+}
+
 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
   return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
                                              Result);
Index: lib/Object/COFFObjectFile.cpp
===================================================================
--- lib/Object/COFFObjectFile.cpp	(revision 151528)
+++ lib/Object/COFFObjectFile.cpp	(working copy)
@@ -378,10 +378,25 @@
 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
                                         bool &Result) const {
   const coff_section *sec = toSec(Sec);
+  Result = !strcmp(sec->Name, ".bss");

This is wrong. sec->Name is not always null terminated, this doesn't
check the flags as per [Special Sections]5, and the spec states that
".bss" is not required, but is used by convention.

+  return object_error::success;
+}
+
+error_code COFFObjectFile::isSectionNobits(DataRefImpl Sec,
+                                           bool &Result) const {
+  const coff_section *sec = toSec(Sec);
   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
   return object_error::success;
 }

Same comments here as above. What do you mean by nobits?

+error_code COFFObjectFile::isSectionExecutable(DataRefImpl Sec,
+                                               bool &Result) const {
+  const coff_section *sec = toSec(Sec);
+  Result = sec->Characteristics & (COFF::IMAGE_SCN_CNT_CODE |
+                                   COFF::IMAGE_SCN_MEM_EXECUTE);

Also needs IMAGE_SCN_MEM_READ.

+  return object_error::success;
+}
+
 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
                                                  DataRefImpl Symb,
                                                  bool &Result) const {
Index: lib/Object/MachOObjectFile.cpp
===================================================================
--- lib/Object/MachOObjectFile.cpp	(revision 151528)
+++ lib/Object/MachOObjectFile.cpp	(working copy)
@@ -554,18 +554,62 @@

 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
                                           bool &Result) const {
-  // FIXME: Unimplemented.
-  Result = false;
+  if (is64BitLoadCommand(MachOObj, DRI)) {
+    InMemoryStruct<macho::Section64> Sect;
+    getSection64(DRI, Sect);
+    Result = !strcmp(Sect->Name, "__data");
+  } else {
+    InMemoryStruct<macho::Section> Sect;
+    getSection(DRI, Sect);
+    Result = !strcmp(Sect->Name, "__data");
+  }
   return object_error::success;
 }

 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
                                          bool &Result) const {
-  // FIXME: Unimplemented.
-  Result = false;
+  if (is64BitLoadCommand(MachOObj, DRI)) {
+    InMemoryStruct<macho::Section64> Sect;
+    getSection64(DRI, Sect);
+    Result = !strcmp(Sect->Name, "__bss");

Again, strcmp is bad. Same below.

+  } else {
+    InMemoryStruct<macho::Section> Sect;
+    getSection(DRI, Sect);
+    Result = !strcmp(Sect->Name, "__bss");
+  }
   return object_error::success;
 }

+error_code MachOObjectFile::isSectionNobits(DataRefImpl DRI,
+                                            bool &Result) const {
+  if (is64BitLoadCommand(MachOObj, DRI)) {
+    InMemoryStruct<macho::Section64> Sect;
+    getSection64(DRI, Sect);
+    Result = !strcmp(Sect->Name, "__bss") || !strcmp(Sect->Name, "__common");
+  } else {
+    InMemoryStruct<macho::Section> Sect;
+    getSection(DRI, Sect);
+    Result = !strcmp(Sect->Name, "__bss") || !strcmp(Sect->Name, "__common");
+  }
+  return object_error::success;
+}
+
+error_code MachOObjectFile::isSectionExecutable(DataRefImpl DRI,
+                                                bool &Result) const {
+  if (is64BitLoadCommand(MachOObj, DRI)) {
+    InMemoryStruct<macho::Section64> Sect;
+    getSection64(DRI, Sect);
+    Result = (Sect->Flags & (MachO::SectionAttrUserPureInstructions |
+                             MachO::SectionAttrSytemSomeInstructions));
+  } else {
+    InMemoryStruct<macho::Section> Sect;
+    getSection(DRI, Sect);
+    Result = (Sect->Flags & (MachO::SectionAttrUserPureInstructions |
+                             MachO::SectionAttrSytemSomeInstructions));
+  }
+  return object_error::success;
+}
+
 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
                                                   DataRefImpl Symb,
                                                   bool &Result) const {

- Michael Spencer




More information about the llvm-commits mailing list