[PATCH] D99754: [LLD][ELF][AVR] Propagate ELF flags to the linked image

LemonBoy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 1 11:13:05 PDT 2021


LemonBoy created this revision.
LemonBoy added reviewers: MaskRay, aykevl, dylanmckay.
LemonBoy added a project: lld.
Herald added subscribers: Jim, arichardson, emaste.
LemonBoy requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The `e_flags` for a ELF file targeting the AVR ISA contains two fields at the time of writing:

- A 7-bit integer field specifying the ISA revision being targeted
- A 1-bit flag specifying whether the object files being linked are suited for applying the relaxations at link time

The linked ELF file is blessed with the arch revision shared among all the files.
The behaviour in case of mismatch is purposefully different than the one implemented in libbfd: LLD will raise a fatal error while libbfd silently picks a default value of `avr2`.
The relaxation-ready flag is handled as done by libbfd, in order for it to appear in the linked object every source object must be tagged with it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99754

Files:
  lld/ELF/Arch/AVR.cpp
  llvm/include/llvm/BinaryFormat/ELF.h


Index: llvm/include/llvm/BinaryFormat/ELF.h
===================================================================
--- llvm/include/llvm/BinaryFormat/ELF.h
+++ llvm/include/llvm/BinaryFormat/ELF.h
@@ -484,7 +484,12 @@
   EF_AVR_ARCH_XMEGA4 = 104,
   EF_AVR_ARCH_XMEGA5 = 105,
   EF_AVR_ARCH_XMEGA6 = 106,
-  EF_AVR_ARCH_XMEGA7 = 107
+  EF_AVR_ARCH_XMEGA7 = 107,
+
+  EF_AVR_ARCH_MASK = 0x7f, // EF_AVR_ARCH_xxx selection mask
+
+  EF_AVR_LINKRELAX_PREPARED = 0x80, // The file is prepared for linker
+                                    // relaxation to be applied
 };
 
 // ELF Relocation types for AVR
Index: lld/ELF/Arch/AVR.cpp
===================================================================
--- lld/ELF/Arch/AVR.cpp
+++ lld/ELF/Arch/AVR.cpp
@@ -43,6 +43,7 @@
 class AVR final : public TargetInfo {
 public:
   AVR();
+  uint32_t calcEFlags() const override;
   RelExpr getRelExpr(RelType type, const Symbol &s,
                      const uint8_t *loc) const override;
   void relocate(uint8_t *loc, const Relocation &rel,
@@ -196,3 +197,30 @@
   static AVR target;
   return ⌖
 }
+
+static uint32_t getEFlags(InputFile *file) {
+  return cast<ObjFile<ELF32LE>>(file)->getObj().getHeader().e_flags;
+}
+
+uint32_t AVR::calcEFlags() const {
+  assert(!objectFiles.empty());
+
+  uint32_t flags = getEFlags(objectFiles[0]);
+  bool hasLinkerRelaxFlag = flags & EF_AVR_LINKRELAX_PREPARED;
+
+  for (InputFile *f : makeArrayRef(objectFiles).slice(1)) {
+    uint32_t obj_flags = getEFlags(f);
+
+    if ((obj_flags & EF_AVR_ARCH_MASK) != (flags & EF_AVR_ARCH_MASK))
+      error(toString(f) +
+            ": cannot link object files with incompatible target ISA");
+
+    if (!(obj_flags & EF_AVR_LINKRELAX_PREPARED))
+      hasLinkerRelaxFlag = false;
+  }
+
+  if (!hasLinkerRelaxFlag)
+    flags &= ~EF_AVR_LINKRELAX_PREPARED;
+
+  return flags;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99754.334769.patch
Type: text/x-patch
Size: 1862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210401/9d42ac33/attachment.bin>


More information about the llvm-commits mailing list