Difference between revisions of "NaplesPU LLD Linker"

From NaplesPU Documentation
Jump to: navigation, search
(elf2hex)
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
When adding a new target architecture to llvm, some changes are required to the tools located in the llvm/tools folder.
+
LLD is the LLVM proprietary linker. It is based on the GNU linker, and it is compatible to it in terms of arguments accepted. It also fully supports the ''ELF'' generation but also, in a progressive lower percentage, ''PE/COFF'', ''Mach-O'', ''WebAssembly'' and custom linker-scripts.
== llvm-objdump ==
+
LLD is built as a set of libraries to join the main idea of the software modular design.
This is the llvm object file dumper. The full documentation for llvm-objdump is maintained as a Texinfo manual. If the info and llvm-objdump programs are properly installed, the command
 
<code> info llvm-objdump </code> should give you access to the complete manual.
 
  
In order to be properly used with the NuPlus architecture the following change is required.
+
The NaplesPU implementation of LLD is made on an ELF-based approach, by adding a new Target class inside lld/ELF/Arch folder. [[NaplesPU.cpp]] contains the ''relocation'' logic and other information needed to support NaplesPU. Each NaplesPU application must be linked together with two additional object files:
  
<syntaxhighlight lang="cpp" line='line'>
+
* ''crt0.o'', is the object file resulting of the compilation of the start routine for the custom platform, composed of operations needed to set-up the platform.
template <class ELFT>
+
* ''vectors.o'', is the Interrupt Vector Table.
static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
+
 
                                                const RelocationRef &RelRef,
+
== Relocations ==
                                                SmallVectorImpl<char> &Result) {
+
This section explains how the relocations are implemented in NaplesPU. 
  ...
+
All relocations are specified in the ''relocateOne'' method implementation. By looking at the target ISA, LLD must check for each instruction type in which an immediate value is specified to verify if it can fit the machine instruction field.
  switch (EF.getHeader()->e_machine) {
+
 
  ...
+
<syntaxhighlight>
  case ELF::EM_NUPLUS:
+
void NaplesPU::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
    res = Target;
+
  int64_t Offset;
    break;
+
  switch (Type) {
 +
  default:
 +
    fatal("unrecognized reloc " + Twine(Type));
 +
    case R_NUPLUS_ABS32:
 +
    write32le(Loc, Val);
 +
    break;
 +
  case R_NUPLUS_BRANCH:
 +
    //Check Offset for J-Type Instructions
 +
    checkInt(Loc, Val, 18, Type);
 +
    applyNaplesPUReloc<18, 0>(Loc, Type, Val);
 +
    break;
 +
  case R_NUPLUS_PCREL_MEM:
 +
    //Check Offset for M-Type Instructions
 +
    checkInt(Loc, Val, 9, Type);
 +
    applyNaplesPUReloc<9, 3>(Loc, Type, Val);
 +
    break;
 +
  case R_NUPLUS_PCREL_LEA:
 +
    //Check Offset for I-Type Instructions
 +
    checkInt(Loc, Val, 9, Type);
 +
    applyNaplesPUReloc<9, 3>(Loc, Type, Val);
 +
    break;
 +
  case R_NUPLUS_ABS_HIGH_LEA:
 +
    //Check Offset for MOVEI-Type Instructions
 +
    checkInt(Loc, (Val >> 16) & 0xFFFF , 16, Type);
 +
    applyNaplesPUReloc<16, 2>(Loc, Type, (Val >> 16) & 0xFFFF);
 +
    break;
 +
  case R_NUPLUS_ABS_LOW_LEA:
 +
    //Check Offset for MOVEI-Type Instructions
 +
    checkInt(Loc, (Val) & 0xFFFF , 16, Type);
 +
    applyNaplesPUReloc<16, 2>(Loc, Type, (Val) & 0xFFFF);
 +
    break;
 +
  }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== llvm-readobj ==
+
All of these implemented relocations relies on the method ''applyNaplesPURelocation'' that simply writes back the value into the instruction.
The llvm-readobj tool displays low-level format-specific information about one or more object files.
+
 
In order to be properly used with the NuPlus architecture the following change is required.
+
== Linkerscript ==
  
<syntaxhighlight lang="cpp" line='line'>
+
All memory images generated for NaplesPU must be linked in a proper way. This way is specified by the linkerscript:
static const EnumEntry<unsigned> ElfMachineType[] = {
+
 
   ...
+
<syntaxhighlight>
  ENUM_ENT(EM_NUPLUS,        "EM_NUPLUS")
+
stacks_dim = 16K;
</syntaxhighlight>
+
stacks_base = 0x80000;
 +
threads_per_core = 0x8;
 +
 
 +
SECTIONS
 +
{
 +
   . = 0x00000380;
 +
  .text.vectors : {
 +
    *vectors.o(.text)
 +
    . = 0x00000400;
 +
  }
  
== linker ==
+
  .text.start : {
[https://github.com/mclinker/mclinker mclinker] is the linker used with the LLVM ver. 3.9. However, it is not under active development so we must migrate towards [http://lld.llvm.org/ LLD].  
+
    *crt0.o(.text)
 +
  }
 +
  .text : { *(.text) }
  
The linker main purpose is to link object files in order to generate an executable file. It must resolve symbol names and relocations in order to produce a proper executable.  
+
  . = 0x00800000;
 +
  .data : { *(.data) }
  
The nu+ version of mclinker is located in "/compiler/tools". The modifications reside in the "NuPlus" folder at the "mclinker/lib/Target" subdirectory.
+
  . = ALIGN(64);
 +
  scratchpad : { *(scratchpad) }
  
The main modifications are the relocations and the scratchpad symbols resolution.  
+
  .bss : { *(.bss) }
The compiler is responsible of emitting these symbols, while the linker must recognize and resolve them by substituting with the correct offset. This is done through a different functions defined in the '''NuPlusRelocator''' class. The symbol-function binding is don in the
 
NuPlusRelocationFunctions.h file.
 
  
To correctly link the different sections, it is also created a [https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/scripts.html linker script]. It is located in "/misc/lnkrscrpt.ld". Other then instruct the linker on where the different sections must be located, it also defines the stack starting address for each core thread. the stacks are defined by a starting address and their dimension.
+
  .eh_frame : { *(.eh_frame) }
Note that mclinker does not correctly support the linker script syntax, so the stacks definition for the mclinker liner script is a workaround. A better solution is to define them in the "SECTIONS" section.
 
  
== elf2hex ==
+
}
The elf2hex tool (located in "/compiler/tools/elf2hex/elf2hex.cpp") takes the elf executable and converts it in hex format. It just takes the data and instructions from the elf file and print it to the output file. The file generated can be used as input to [[Emulator | EmuPlus]]. However, the file requires some other transformations in order to be loaded to the real machine. The 512_hex python program ("/misc/512_hex.py") is responsible of execute these transformations.
+
</syntaxhighlight>
  
== lldb ==
+
As shown above, the linker script commonly defines where the sections must be placed into the memory. Also, it solves undefined symbols of the start routine, specifying the stack size per-thread, the number of threads and the starting address for the stack frame.
TBD
 

Latest revision as of 16:21, 21 June 2019

LLD is the LLVM proprietary linker. It is based on the GNU linker, and it is compatible to it in terms of arguments accepted. It also fully supports the ELF generation but also, in a progressive lower percentage, PE/COFF, Mach-O, WebAssembly and custom linker-scripts. LLD is built as a set of libraries to join the main idea of the software modular design.

The NaplesPU implementation of LLD is made on an ELF-based approach, by adding a new Target class inside lld/ELF/Arch folder. NaplesPU.cpp contains the relocation logic and other information needed to support NaplesPU. Each NaplesPU application must be linked together with two additional object files:

  • crt0.o, is the object file resulting of the compilation of the start routine for the custom platform, composed of operations needed to set-up the platform.
  • vectors.o, is the Interrupt Vector Table.

Relocations

This section explains how the relocations are implemented in NaplesPU. All relocations are specified in the relocateOne method implementation. By looking at the target ISA, LLD must check for each instruction type in which an immediate value is specified to verify if it can fit the machine instruction field.

void NaplesPU::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
  int64_t Offset;
  switch (Type) {
  default:
    fatal("unrecognized reloc " + Twine(Type));
    case R_NUPLUS_ABS32:
     write32le(Loc, Val);
     break;
   case R_NUPLUS_BRANCH:
     //Check Offset for J-Type Instructions
     checkInt(Loc, Val, 18, Type);
     applyNaplesPUReloc<18, 0>(Loc, Type, Val);
     break;
   case R_NUPLUS_PCREL_MEM:
     //Check Offset for M-Type Instructions
     checkInt(Loc, Val, 9, Type);
     applyNaplesPUReloc<9, 3>(Loc, Type, Val);
     break;
   case R_NUPLUS_PCREL_LEA:
     //Check Offset for I-Type Instructions
     checkInt(Loc, Val, 9, Type);
     applyNaplesPUReloc<9, 3>(Loc, Type, Val);
     break;
   case R_NUPLUS_ABS_HIGH_LEA:
     //Check Offset for MOVEI-Type Instructions
     checkInt(Loc, (Val >> 16) & 0xFFFF , 16, Type);
     applyNaplesPUReloc<16, 2>(Loc, Type, (Val >> 16) & 0xFFFF);
     break;
   case R_NUPLUS_ABS_LOW_LEA:
     //Check Offset for MOVEI-Type Instructions
     checkInt(Loc, (Val) & 0xFFFF , 16, Type);
     applyNaplesPUReloc<16, 2>(Loc, Type, (Val) & 0xFFFF);
     break;
  }
}

All of these implemented relocations relies on the method applyNaplesPURelocation that simply writes back the value into the instruction.

Linkerscript

All memory images generated for NaplesPU must be linked in a proper way. This way is specified by the linkerscript:

stacks_dim = 16K;
stacks_base = 0x80000;
threads_per_core = 0x8;

SECTIONS
{
  . = 0x00000380;
  .text.vectors : {
    *vectors.o(.text)
    . = 0x00000400;
  }

  .text.start : {
    *crt0.o(.text)
  }
  .text : { *(.text) }

  . = 0x00800000;
  .data : { *(.data) }

  . = ALIGN(64);
  scratchpad : { *(scratchpad) }

  .bss : { *(.bss) }

  .eh_frame : { *(.eh_frame) }

}

As shown above, the linker script commonly defines where the sections must be placed into the memory. Also, it solves undefined symbols of the start routine, specifying the stack size per-thread, the number of threads and the starting address for the stack frame.