[llvm] 2d59ed4 - [yaml2obj] - Add a way to override the sh_addralign field of a section.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 27 03:04:05 PDT 2020


Author: Georgii Rymar
Date: 2020-10-27T13:03:38+03:00
New Revision: 2d59ed4e62a9842be428f32976760dbb9cf336af

URL: https://github.com/llvm/llvm-project/commit/2d59ed4e62a9842be428f32976760dbb9cf336af
DIFF: https://github.com/llvm/llvm-project/commit/2d59ed4e62a9842be428f32976760dbb9cf336af.diff

LOG: [yaml2obj] - Add a way to override the sh_addralign field of a section.

Imagine the following declaration of a section:
```
Sections:
  - Name:         .dynsym
    Type:         SHT_DYNSYM
    AddressAlign: 0x1111111111111111
```

The aligment is large and yaml2obj reports an error currently:
"the desired output size is greater than permitted. Use the --max-size option to change the limit"

This patch implements the "ShAddrAlign" key, which is similar to other "Sh*" keys we have.
With it it is possible to override the `sh_addralign` field, ignoring the writing of alignment bytes.

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

Added: 
    llvm/test/tools/yaml2obj/ELF/override-shaddralign.yaml

Modified: 
    llvm/include/llvm/ObjectYAML/ELFYAML.h
    llvm/lib/ObjectYAML/ELFEmitter.cpp
    llvm/lib/ObjectYAML/ELFYAML.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h
index 28cbf325ecb6..f3805a91853b 100644
--- a/llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -203,6 +203,9 @@ struct Section : public Chunk {
   // The following members are used to override section fields which is
   // useful for creating invalid objects.
 
+  // This can be used to override the sh_addralign field.
+  Optional<llvm::yaml::Hex64> ShAddrAlign;
+
   // This can be used to override the offset stored in the sh_name field.
   // It does not affect the name stored in the string table.
   Optional<llvm::yaml::Hex64> ShName;

diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 4474afbc3255..b811ccec06a3 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -552,6 +552,8 @@ template <class ELFT>
 static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) {
   if (!From)
     return;
+  if (From->ShAddrAlign)
+    To.sh_addralign = *From->ShAddrAlign;
   if (From->ShFlags)
     To.sh_flags = *From->ShFlags;
   if (From->ShName)

diff  --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 978c4968dc64..cd4fd338d6ec 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1113,9 +1113,9 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
   // are producing YAML, because yaml2obj sets appropriate values for them
   // automatically when they are not explicitly defined.
   assert(!IO.outputting() ||
-         (!Section.ShOffset.hasValue() && !Section.ShSize.hasValue() &&
-          !Section.ShName.hasValue() && !Section.ShFlags.hasValue() &&
-          !Section.ShType.hasValue()));
+         (!Section.ShOffset && !Section.ShSize && !Section.ShName &&
+          !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign));
+  IO.mapOptional("ShAddrAlign", Section.ShAddrAlign);
   IO.mapOptional("ShName", Section.ShName);
   IO.mapOptional("ShOffset", Section.ShOffset);
   IO.mapOptional("ShSize", Section.ShSize);

diff  --git a/llvm/test/tools/yaml2obj/ELF/override-shaddralign.yaml b/llvm/test/tools/yaml2obj/ELF/override-shaddralign.yaml
new file mode 100644
index 000000000000..62578c1e2dea
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/ELF/override-shaddralign.yaml
@@ -0,0 +1,30 @@
+## Check we are able to set a custom sh_addralign field for 
diff erent sections
+## and that doing this does not affect the output size.
+
+## Test that we are able to override the sh_addralign section
+## field with use of the "ShAddrAlign" key.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-readelf --sections %t | FileCheck %s --check-prefix=CHECK
+
+# CHECK:      Section Headers:
+# CHECK-NEXT:   [Nr] Name    {{.*}} Off    Size   ES Flg Lk Inf Al
+# CHECK-NEXT:   [ 0]         {{.*}} 000000 000000 00     0   0  0
+# CHECK-NEXT:   [ 1] .foo    {{.*}} 000080 000000 00     0   0 1229782938247303441
+# CHECK-NEXT:   [ 2] .bar    {{.*}} 000100 000000 00     0   0 2459565876494606882
+# CHECK-NEXT:   [ 3] .strtab {{.*}} 000100 000001 00     0   0  1
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Sections:
+  - Name:         .foo
+    Type:         SHT_PROGBITS
+    AddressAlign: 0x80
+    ShAddrAlign:  0x1111111111111111
+  - Name:         .bar
+    Type:         SHT_PROGBITS
+    AddressAlign: 0x100
+    ShAddrAlign:  0x2222222222222222


        


More information about the llvm-commits mailing list