[lld] r244050 - Add bare minimum to get a working i386 linux program.

Rafael Espindola rafael.espindola at gmail.com
Wed Aug 5 08:08:40 PDT 2015


Author: rafael
Date: Wed Aug  5 10:08:40 2015
New Revision: 244050

URL: http://llvm.org/viewvc/llvm-project?rev=244050&view=rev
Log:
Add bare minimum to get a working i386 linux program.

Added:
    lld/trunk/test/elf2/basic32.s
Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=244050&r1=244049&r2=244050&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Wed Aug  5 10:08:40 2015
@@ -114,6 +114,27 @@ void LinkerDriver::link(ArrayRef<const c
   Symtab.reportRemainingUndefines();
 
   // Write the result.
-  Writer<object::ELF64LE> Out(&Symtab);
-  Out.write(Config->OutputFile);
+  ObjectFileBase &FirstObj = *Symtab.ObjectFiles[0];
+  switch (FirstObj.kind()) {
+  case InputFile::Object32LEKind: {
+    Writer<object::ELF32LE> Out(&Symtab);
+    Out.write(Config->OutputFile);
+    return;
+  }
+  case InputFile::Object32BEKind: {
+    Writer<object::ELF32BE> Out(&Symtab);
+    Out.write(Config->OutputFile);
+    return;
+  }
+  case InputFile::Object64LEKind: {
+    Writer<object::ELF64LE> Out(&Symtab);
+    Out.write(Config->OutputFile);
+    return;
+  }
+  case InputFile::Object64BEKind: {
+    Writer<object::ELF64BE> Out(&Symtab);
+    Out.write(Config->OutputFile);
+    return;
+  }
+  }
 }

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=244050&r1=244049&r2=244050&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Aug  5 10:08:40 2015
@@ -119,13 +119,16 @@ template <class ELFT> void Writer<ELFT>:
   EHdr->e_ident[EI_MAG1] = 0x45;
   EHdr->e_ident[EI_MAG2] = 0x4C;
   EHdr->e_ident[EI_MAG3] = 0x46;
-  EHdr->e_ident[EI_CLASS] = ELFCLASS64;
-  EHdr->e_ident[EI_DATA] = ELFDATA2LSB;
+  EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
+  EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little
+                               ? ELFDATA2LSB
+                               : ELFDATA2MSB;
   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
   EHdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
 
   EHdr->e_type = ET_EXEC;
-  EHdr->e_machine = EM_X86_64;
+  auto &FirstObj = cast<ObjectFile<ELFT>>(*Symtab->ObjectFiles[0]);
+  EHdr->e_machine = FirstObj.getObj()->getHeader()->e_machine;
   EHdr->e_version = EV_CURRENT;
   EHdr->e_entry = 0x401000;
   EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>);

Added: lld/trunk/test/elf2/basic32.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic32.s?rev=244050&view=auto
==============================================================================
--- lld/trunk/test/elf2/basic32.s (added)
+++ lld/trunk/test/elf2/basic32.s Wed Aug  5 10:08:40 2015
@@ -0,0 +1,116 @@
+# RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t
+# RUN: lld -flavor gnu2 %t -o %t2
+# RUN: llvm-readobj -file-headers -sections -program-headers %t2 | FileCheck %s
+# REQUIRES: x86
+
+# exits with return code 42 on linux
+.globl _start;
+_start:
+  mov $1, %eax
+  mov $42, %ebx
+  int $0x80
+
+# CHECK:      ElfHeader {
+# CHECK-NEXT:   Ident {
+# CHECK-NEXT:     Magic: (7F 45 4C 46)
+# CHECK-NEXT:     Class: 32-bit (0x1)
+# CHECK-NEXT:     DataEncoding: LittleEndian (0x1)
+# CHECK-NEXT:     FileVersion: 1
+# CHECK-NEXT:     OS/ABI: SystemV (0x0)
+# CHECK-NEXT:     ABIVersion: 0
+# CHECK-NEXT:     Unused: (00 00 00 00 00 00 00)
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Type: Executable (0x2)
+# CHECK-NEXT:   Machine: EM_386 (0x3)
+# CHECK-NEXT:   Version: 1
+# CHECK-NEXT:   Entry: 0x401000
+# CHECK-NEXT:   ProgramHeaderOffset: 0x34
+# CHECK-NEXT:   SectionHeaderOffset: 0x1010
+# CHECK-NEXT:   Flags [ (0x0)
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   HeaderSize: 52
+# CHECK-NEXT:   ProgramHeaderEntrySize: 32
+# CHECK-NEXT:   ProgramHeaderCount: 1
+# CHECK-NEXT:   SectionHeaderEntrySize: 40
+# CHECK-NEXT:   SectionHeaderCount: 4
+# CHECK-NEXT:    StringTableSectionIndex: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Sections [
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 0
+# CHECK-NEXT:     Name:  (0)
+# CHECK-NEXT:     Type: SHT_NULL (0x0)
+# CHECK-NEXT:     Flags [ (0x0)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset: 0x0
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 0
+# CHECK-NEXT:     Info: 0
+# CHECK-NEXT:     AddressAlignment: 0
+# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 1
+# CHECK-NEXT:     Name:  (0)
+# CHECK-NEXT:     Type: SHT_PROGBITS (0x1)
+# CHECK-NEXT:     Flags [ (0x6)
+# CHECK-NEXT:       SHF_ALLOC (0x2)
+# CHECK-NEXT:       SHF_EXECINSTR (0x4)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x1000
+# CHECK-NEXT:     Offset: 0x1000
+# CHECK-NEXT:     Size: 12
+# CHECK-NEXT:     Link: 0
+# CHECK-NEXT:     Info: 0
+# CHECK-NEXT:     AddressAlignment: 0
+# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 2
+# CHECK-NEXT:     Name:  (0)
+# CHECK-NEXT:     Type: SHT_PROGBITS (0x1)
+# CHECK-NEXT:     Flags [ (0x3)
+# CHECK-NEXT:       SHF_ALLOC (0x2)
+# CHECK-NEXT:       SHF_WRITE (0x1)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x2000
+# CHECK-NEXT:     Offset: 0x0
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 0
+# CHECK-NEXT:     Info: 0
+# CHECK-NEXT:     AddressAlignment: 0
+# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 3
+# CHECK-NEXT:     Name:  (0)
+# CHECK-NEXT:     Type: SHT_NOBITS (0x8)
+# CHECK-NEXT:     Flags [ (0x3)
+# CHECK-NEXT:       SHF_ALLOC (0x2)
+# CHECK-NEXT:       SHF_WRITE (0x1)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x2000
+# CHECK-NEXT:     Offset: 0x0
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 0
+# CHECK-NEXT:     Info: 0
+# CHECK-NEXT:     AddressAlignment: 0
+# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+# CHECK-NEXT: ProgramHeaders [
+# CHECK-NEXT:   ProgramHeader {
+# CHECK-NEXT:     Type: PT_LOAD (0x1)
+# CHECK-NEXT:     Offset: 0x0
+# CHECK-NEXT:     VirtualAddress: 0x400000
+# CHECK-NEXT:     PhysicalAddress: 0x400000
+# CHECK-NEXT:     FileSize: 4272
+# CHECK-NEXT:     MemSize: 4272
+# CHECK-NEXT:     Flags [ (0x5)
+# CHECK-NEXT:       PF_R (0x4)
+# CHECK-NEXT:       PF_X (0x1)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Alignment: 16384
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]




More information about the llvm-commits mailing list