Browse Source

* Changed TObjSection.WriteStr method to write a zero byte after the string.
+ Added TObjSection.WriteBytes method that writes just a string (without zero byte).
* ogelf.pas: Fixed st_value of COMMON symbols, it must contain required symbol alignment.
+ ogelf.pas: Some more definitions.

git-svn-id: trunk@22332 -

sergei 13 years ago
parent
commit
c41b407119
3 changed files with 38 additions and 12 deletions
  1. 13 0
      compiler/ogbase.pas
  2. 0 2
      compiler/ogcoff.pas
  3. 25 10
      compiler/ogelf.pas

+ 13 - 0
compiler/ogbase.pas

@@ -207,8 +207,11 @@ interface
        constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);virtual;
        constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);virtual;
        destructor  destroy;override;
        destructor  destroy;override;
        function  write(const d;l:aword):aword;
        function  write(const d;l:aword):aword;
+       { writes string plus zero byte }
        function  writestr(const s:string):aword;
        function  writestr(const s:string):aword;
        function  WriteZeros(l:longword):aword;
        function  WriteZeros(l:longword):aword;
+       { writes content of s without null termination }
+       function  WriteBytes(const s:string):aword;
        procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);virtual;
        procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);virtual;
        function  setmempos(mpos:qword):qword;
        function  setmempos(mpos:qword):qword;
        procedure setDatapos(var dpos:aword);
        procedure setDatapos(var dpos:aword);
@@ -707,6 +710,16 @@ implementation
 
 
 
 
     function TObjSection.writestr(const s:string):aword;
     function TObjSection.writestr(const s:string):aword;
+      var
+        b: byte;
+      begin
+        result:=Write(s[1],length(s));
+        b:=0;
+        Write(b,1);
+      end;
+
+
+    function TObjSection.WriteBytes(const s:string):aword;
       begin
       begin
         result:=Write(s[1],length(s));
         result:=Write(s[1],length(s));
       end;
       end;

+ 0 - 2
compiler/ogcoff.pas

@@ -2428,7 +2428,6 @@ const pemagic : array[0..3] of byte = (
           idata2objsection.writereloc_internal(idata5objsection,0,sizeof(longint),RELOC_RVA);
           idata2objsection.writereloc_internal(idata5objsection,0,sizeof(longint),RELOC_RVA);
           { idata7 }
           { idata7 }
           idata7objsection.writestr(basedllname);
           idata7objsection.writestr(basedllname);
-          idata7objsection.writezeros(1);
         end;
         end;
 
 
         procedure EndImport;
         procedure EndImport;
@@ -2480,7 +2479,6 @@ const pemagic : array[0..3] of byte = (
                     { index hint, function name, null terminator and align }
                     { index hint, function name, null terminator and align }
                     idata6objsection.write(ordint,2);
                     idata6objsection.write(ordint,2);
                     idata6objsection.writestr(afuncname);
                     idata6objsection.writestr(afuncname);
-                    idata6objsection.writezeros(1);
                     idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
                     idata6objsection.writezeros(align(idata6objsection.size,2)-idata6objsection.size);
                   end;
                   end;
                 objsec.writereloc_internal(idata6objsection,0,sizeof(longint),RELOC_RVA);
                 objsec.writereloc_internal(idata6objsection,0,sizeof(longint),RELOC_RVA);

+ 25 - 10
compiler/ogelf.pas

@@ -120,6 +120,7 @@ implementation
       EM_SPARC      = 2;
       EM_SPARC      = 2;
       EM_386        = 3;
       EM_386        = 3;
       EM_M68K       = 4;
       EM_M68K       = 4;
+      EM_MIPS       = 8;
       EM_PPC        = 20;
       EM_PPC        = 20;
       EM_ARM        = 40;
       EM_ARM        = 40;
       EM_X86_64     = 62;
       EM_X86_64     = 62;
@@ -136,6 +137,12 @@ implementation
 {$ifdef powerpc}
 {$ifdef powerpc}
       ELFMACHINE = EM_PPC;
       ELFMACHINE = EM_PPC;
 {$endif powerpc}
 {$endif powerpc}
+{$ifdef powerpc64}
+      ELFMACHINE = EM_PPC; // TODO
+{$endif}
+{$ifdef mips}
+      ELFMACHINE = EM_MIPS;
+{$endif}
 {$ifdef arm}
 {$ifdef arm}
       ELFMACHINE = EM_ARM;
       ELFMACHINE = EM_ARM;
 {$endif arm}
 {$endif arm}
@@ -159,10 +166,22 @@ implementation
       SHT_REL      = 9;
       SHT_REL      = 9;
       SHT_SHLIB    = 10;
       SHT_SHLIB    = 10;
       SHT_DYNSYM   = 11;
       SHT_DYNSYM   = 11;
+      SHT_INIT_ARRAY = 14;
+      SHT_FINI_ARRAY = 15;
+      SHT_PREINIT_ARRAY = 16;
+      SHT_GROUP    = 17;
+      SHT_SYMTAB_SHNDX = 18;
 
 
       SHF_WRITE     = 1;
       SHF_WRITE     = 1;
       SHF_ALLOC     = 2;
       SHF_ALLOC     = 2;
       SHF_EXECINSTR = 4;
       SHF_EXECINSTR = 4;
+      SHF_MERGE     = 16;
+      SHF_STRINGS   = 32;
+      SHF_INFO_LINK = 64;
+      SHF_LINK_ORDER = 128;
+      SHF_OS_NONCONFORMING = 256;
+      SHF_GROUP     = 512;
+      SHF_TLS       = 1024;
 
 
       STB_LOCAL   = 0;
       STB_LOCAL   = 0;
       STB_GLOBAL  = 1;
       STB_GLOBAL  = 1;
@@ -185,6 +204,7 @@ implementation
       PT_NOTE     = 4;
       PT_NOTE     = 4;
       PT_SHLIB    = 5;
       PT_SHLIB    = 5;
       PT_PHDR     = 6;
       PT_PHDR     = 6;
+      PT_TLS      = 7;
       PT_LOOS     = $60000000;
       PT_LOOS     = $60000000;
       PT_HIOS     = $6FFFFFFF;
       PT_HIOS     = $6FFFFFFF;
       PT_LOPROC   = $70000000;
       PT_LOPROC   = $70000000;
@@ -988,7 +1008,7 @@ implementation
         dyn:=(aKind=esk_dyn);
         dyn:=(aKind=esk_dyn);
         create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],sizeof(pint),sizeof(TElfSymbol));
         create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],sizeof(pint),sizeof(TElfSymbol));
         fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],1,0);
         fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],1,0);
-        fstrsec.writestr(#0);
+        fstrsec.writezeros(1);
         writezeros(sizeof(TElfSymbol));
         writezeros(sizeof(TElfSymbol));
         symidx:=1;
         symidx:=1;
         shinfo:=1;
         shinfo:=1;
@@ -1015,12 +1035,8 @@ implementation
         elfsym:TElfSymbol;
         elfsym:TElfSymbol;
       begin
       begin
         fillchar(elfsym,sizeof(elfsym),0);
         fillchar(elfsym,sizeof(elfsym),0);
-        { symbolname, write the #0 separate to overcome 255+1 char not possible }
         if nameidx=0 then
         if nameidx=0 then
-          begin
-            elfsym.st_name:=fstrsec.writestr(objsym.name);
-            fstrsec.writestr(#0);
-          end
+          elfsym.st_name:=fstrsec.writestr(objsym.name)
         else
         else
           elfsym.st_name:=nameidx;
           elfsym.st_name:=nameidx;
         elfsym.st_size:=objsym.size;
         elfsym.st_size:=objsym.size;
@@ -1033,7 +1049,7 @@ implementation
             end;
             end;
           AB_COMMON :
           AB_COMMON :
             begin
             begin
-              elfsym.st_value:=$10;            { ?? should not be hardcoded }
+              elfsym.st_value:=var_align(objsym.size);
               elfsym.st_info:=STB_GLOBAL shl 4;
               elfsym.st_info:=STB_GLOBAL shl 4;
               elfsym.st_shndx:=SHN_COMMON;
               elfsym.st_shndx:=SHN_COMMON;
             end;
             end;
@@ -1182,7 +1198,7 @@ implementation
 
 
     procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
     procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
       begin
       begin
-        TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name+#0);
+        TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name);
       end;
       end;
 
 
 
 
@@ -1190,7 +1206,7 @@ implementation
       begin
       begin
         with data do
         with data do
          begin
          begin
-           shstrtabsect.writestr(#0);
+           shstrtabsect.writezeros(1);
            ObjSectionList.ForEachCall(@section_write_sh_string,nil);
            ObjSectionList.ForEachCall(@section_write_sh_string,nil);
          end;
          end;
       end;
       end;
@@ -1254,7 +1270,6 @@ implementation
              TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,1,0);
              TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,1,0);
            { insert filename as first in strtab }
            { insert filename as first in strtab }
            symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
            symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
-           symtabsect.fstrsec.writestr(#0);
            { calc amount of sections we have }
            { calc amount of sections we have }
            nsections:=1;
            nsections:=1;
            { also create the index in the section header table }
            { also create the index in the section header table }