Browse Source

* order of named sections added
* fix debugstart and debugend labels

git-svn-id: trunk@5098 -

peter 19 years ago
parent
commit
070b86a512
9 changed files with 181 additions and 193 deletions
  1. 2 0
      compiler/aasmbase.pas
  2. 10 8
      compiler/aasmtai.pas
  3. 23 12
      compiler/aggas.pas
  4. 70 16
      compiler/assemble.pas
  5. 3 1
      compiler/dbgdwarf.pas
  6. 2 2
      compiler/dbgstabs.pas
  7. 29 25
      compiler/ogbase.pas
  8. 20 84
      compiler/ogcoff.pas
  9. 22 45
      compiler/ogelf.pas

+ 2 - 0
compiler/aasmbase.pas

@@ -77,6 +77,8 @@ interface
          sec_toc
        );
 
+       TAsmSectionOrder = (secorder_begin,secorder_default,secorder_end);
+
        TAsmSymbol = class(TNamedIndexItem)
        private
          { this need to be incremented with every symbol loading into the

+ 10 - 8
compiler/aasmtai.pas

@@ -353,11 +353,12 @@ interface
 
        { Generates a section / segment directive }
        tai_section = class(tai)
-          sectype : TAsmSectiontype;
+          sectype  : TAsmSectiontype;
+          secorder : TasmSectionorder;
           secalign : byte;
-          name    : pstring;
-          sec     : TObjSection; { used in binary writer }
-          constructor Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
+          name     : pstring;
+          sec      : TObjSection; { used in binary writer }
+          constructor Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
           destructor Destroy;override;
           constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
           procedure ppuwrite(ppufile:tcompilerppufile);override;
@@ -635,7 +636,7 @@ interface
       add_reg_instruction_hook : tadd_reg_instruction_proc;
 
     procedure maybe_new_object_file(list:TAsmList);
-    procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
+    procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
     procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
                                    Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
     procedure section_symbol_end(list:TAsmList;const Aname:string);
@@ -666,9 +667,9 @@ implementation
       end;
 
 
-    procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
+    procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
       begin
-        list.concat(tai_section.create(Asectype,Aname,Aalign));
+        list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
         list.concat(cai_align.create(Aalign));
       end;
 
@@ -813,12 +814,13 @@ implementation
                              TAI_SECTION
  ****************************************************************************}
 
-    constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
+    constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
       begin
         inherited Create;
         typ:=ait_section;
         sectype:=asectype;
         secalign:=Aalign;
+        secorder:=Asecorder;
         name:=stringdup(Aname);
         sec:=nil;
       end;

+ 23 - 12
compiler/aggas.pas

@@ -47,8 +47,8 @@ interface
       }
       TGNUAssembler=class(texternalassembler)
       protected
-        function sectionname(atype:TAsmSectiontype;const aname:string):string;virtual;
-        procedure WriteSection(atype:TAsmSectiontype;const aname:string);
+        function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
+        procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
         procedure WriteExtraHeader;virtual;
         procedure WriteInstruction(hp: tai);
        public
@@ -81,7 +81,7 @@ interface
 
 
       TAppleGNUAssembler=class(TGNUAssembler)
-        function sectionname(atype:TAsmSectiontype;const aname:string):string;override;
+        function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
        private
         debugframecount: aint;
        end;
@@ -241,7 +241,7 @@ implementation
         result := target_asm.labelprefix+'$set$'+tostr(setcount);
       end;
 
-    function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string):string;
+    function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
       const
         secnames : array[TAsmSectiontype] of string[17] = ('',
           '.text',
@@ -282,6 +282,7 @@ implementation
           '.toc'
         );
       var
+        sep     : string[3];
         secname : string;
       begin
         if (cs_create_pic in current_settings.moduleswitches) and
@@ -302,13 +303,23 @@ implementation
            (aname<>'') and
            (atype <> sec_toc) and
            (atype<>sec_bss) then
-          result:=secname+'.'+aname
+          begin
+            case aorder of
+              secorder_begin :
+                sep:='.b_';
+              secorder_end :
+                sep:='.z_';
+              else
+                sep:='.n_';
+            end;
+            result:=secname+sep+aname
+          end
         else
           result:=secname;
       end;
 
 
-    procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
+    procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
       var
         s : string;
       begin
@@ -325,7 +336,7 @@ implementation
          else
           AsmWrite('.section ');
         end;
-        s:=sectionname(atype,aname);
+        s:=sectionname(atype,aname,aorder);
         AsmWrite(s);
         case atype of
           sec_fpc :
@@ -551,7 +562,7 @@ implementation
            ait_section :
              begin
                if tai_section(hp).sectype<>sec_none then
-                 WriteSection(tai_section(hp).sectype,tai_section(hp).name^)
+                 WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder)
                else
                  begin
 {$ifdef EXTDEBUG}
@@ -580,7 +591,7 @@ implementation
                        asmwrite(tai_datablock(hp).sym.name);
                        asmwriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
                        if not(CurrSecType in [sec_data,sec_none]) then
-                         writesection(CurrSecType,'');
+                         writesection(CurrSecType,'',secorder_default);
                      end
                    else
                      begin
@@ -977,7 +988,7 @@ implementation
                      hp:=tai(hp.next);
                    end;
                   if CurrSecType<>sec_none then
-                    WriteSection(CurrSecType,'');
+                    WriteSection(CurrSecType,'',secorder_default);
                   AsmStartSize:=AsmSize;
 
                   { reset dwarf file index }
@@ -1079,7 +1090,7 @@ implementation
 {                        Apple/GNU Assembler writer                          }
 {****************************************************************************}
 
-    function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string):string;
+    function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
       begin
         if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
           case atype of
@@ -1100,7 +1111,7 @@ implementation
                 exit;
               end;
           end;
-        result := inherited sectionname(atype,aname);
+        result := inherited sectionname(atype,aname,aorder);
       end;
 
 

+ 70 - 16
compiler/assemble.pas

@@ -140,7 +140,7 @@ interface
         { current processing }
         currlistidx  : byte;
         currlist     : TAsmList;
-        procedure convertstab(p:pchar);
+        procedure WriteStab(p:pchar);
         function  MaybeNextList(var hp:Tai):boolean;
         function  TreePass0(hp:Tai):Tai;
         function  TreePass1(hp:Tai):Tai;
@@ -703,7 +703,7 @@ Implementation
       end;
 
 
-    procedure TInternalAssembler.convertstab(p:pchar);
+    procedure TInternalAssembler.WriteStab(p:pchar);
 
         function consumecomma(var p:pchar):boolean;
         begin
@@ -829,22 +829,26 @@ Implementation
       const
         N_Function = $24; { function or const }
       var
+        stabstrlen,
         ofs,
         nline,
         nidx,
         nother,
         i         : longint;
+        stab      : TObjStabEntry;
         relocsym  : TObjSymbol;
         pstr,
         pcurr,
         pendquote : pchar;
+        oldsec    : TObjSection;
+        reltype   : TObjRelocationType;
       begin
         pcurr:=nil;
         pstr:=nil;
         pendquote:=nil;
 
         { Parse string part }
-        if p[0]='"' then
+        if (p[0]='"') then
           begin
             pstr:=@p[1];
             { Ignore \" inside the string }
@@ -863,7 +867,11 @@ Implementation
 
         { When in pass 1 then only alloc and leave }
         if ObjData.currpass=1 then
-          ObjData.allocstab(pstr)
+          begin
+            ObjData.StabsSec.Alloc(sizeof(TObjStabEntry));
+            if assigned(pstr) and (pstr[0]<>#0) then
+              ObjData.StabStrSec.Alloc(strlen(pstr)+1);
+          end
         else
           begin
             { Stabs format: nidx,nother,nline[,offset] }
@@ -887,7 +895,53 @@ Implementation
             if assigned(relocsym) and
                (relocsym.bind<>AB_LOCAL) then
               ofs:=0;
-            ObjData.writestab(ofs,relocsym,byte(nidx),byte(nother),word(nline),pstr);
+
+            { Generate stab entry }
+            if assigned(pstr) and (pstr[0]<>#0) then
+              begin
+                stabstrlen:=strlen(pstr);
+{$ifdef optimizestabs}
+                StabStrEntry:=nil;
+                if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
+                  begin
+                    hs:=strpas(pstr);
+                    StabstrEntry:=StabStrDict.Search(hs);
+                    if not assigned(StabstrEntry) then
+                      begin
+                        StabstrEntry:=TStabStrEntry.Create(hs);
+                        StabstrEntry:=StabStrSec.Size;
+                        StabStrDict.Insert(StabstrEntry);
+                        { generate new stab }
+                        StabstrEntry:=nil;
+                      end;
+                  end;
+                if assigned(StabstrEntry) then
+                  stab.strpos:=StabstrEntry.strpos
+                else
+{$endif optimizestabs}
+                  begin
+                    stab.strpos:=ObjData.StabStrSec.Size;
+                    ObjData.StabStrSec.write(pstr^,stabstrlen+1);
+                  end;
+              end
+            else
+              stab.strpos:=0;
+            stab.ntype:=byte(nidx);
+            stab.ndesc:=word(nline);
+            stab.nother:=byte(nother);
+            stab.nvalue:=ofs;
+
+            { Write the stab first without the value field. Then
+              write a the value field with relocation }
+            oldsec:=ObjData.CurrObjSec;
+            ObjData.SetSection(ObjData.StabsSec);
+            ObjData.Writebytes(stab,sizeof(TObjStabEntry)-4);
+            if DLLSource and RelocSection then
+              reltype:=RELOC_RVA
+            else
+              reltype:=RELOC_ABSOLUTE;
+            ObjData.Writereloc(stab.nvalue,4,relocsym,reltype);
+            ObjData.setsection(oldsec);
           end;
         if assigned(pendquote) then
           pendquote^:='"';
@@ -945,7 +999,7 @@ Implementation
                ObjData.alloc(tai_const(hp).size);
              ait_section:
                begin
-                 ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^);
+                 ObjData.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secorder);
                  Tai_section(hp).sec:=ObjData.CurrObjSec;
                end;
              ait_symbol :
@@ -1019,7 +1073,7 @@ Implementation
              ait_stab :
                begin
                  if assigned(Tai_stab(hp).str) then
-                   convertstab(Tai_stab(hp).str);
+                   WriteStab(Tai_stab(hp).str);
                end;
              ait_symbol :
                ObjData.SymbolDefine(Tai_symbol(hp).sym);
@@ -1158,7 +1212,7 @@ Implementation
              ait_instruction :
                Taicpu(hp).Pass2(ObjData);
              ait_stab :
-               convertstab(Tai_stab(hp).str);
+               WriteStab(Tai_stab(hp).str);
              ait_function_name,
              ait_force_line : ;
              ait_cutobject :
@@ -1189,7 +1243,7 @@ Implementation
 
         { Pass 0 }
         ObjData.currpass:=0;
-        ObjData.createsection(sec_code,'');
+        ObjData.createsection(sec_code);
         ObjData.beforealloc;
         { start with list 1 }
         currlistidx:=1;
@@ -1209,7 +1263,7 @@ Implementation
         ObjData.currpass:=1;
         ObjData.resetsections;
         ObjData.beforealloc;
-        ObjData.createsection(sec_code,'');
+        ObjData.createsection(sec_code);
         { start with list 1 }
         currlistidx:=1;
         currlist:=list[currlistidx];
@@ -1219,7 +1273,7 @@ Implementation
            hp:=TreePass1(hp);
            MaybeNextList(hp);
          end;
-        ObjData.createsection(sec_code,'');
+        ObjData.createsection(sec_code);
         ObjData.afteralloc;
 
         { leave if errors have occured }
@@ -1230,7 +1284,7 @@ Implementation
         ObjData.currpass:=2;
         ObjData.resetsections;
         ObjData.beforewrite;
-        ObjData.createsection(sec_code,'');
+        ObjData.createsection(sec_code);
         { start with list 1 }
         currlistidx:=1;
         currlist:=list[currlistidx];
@@ -1240,7 +1294,7 @@ Implementation
            hp:=TreePass2(hp);
            MaybeNextList(hp);
          end;
-        ObjData.createsection(sec_code,'');
+        ObjData.createsection(sec_code);
         ObjData.afterwrite;
 
         { don't write the .o file if errors have occured }
@@ -1287,7 +1341,7 @@ Implementation
            ObjData.currpass:=0;
            ObjData.resetsections;
            ObjData.beforealloc;
-           ObjData.createsection(startsectype,'');
+           ObjData.createsection(startsectype);
            TreePass0(hp);
            ObjData.afteralloc;
            { leave if errors have occured }
@@ -1298,7 +1352,7 @@ Implementation
            ObjData.currpass:=1;
            ObjData.resetsections;
            ObjData.beforealloc;
-           ObjData.createsection(startsectype,'');
+           ObjData.createsection(startsectype);
            TreePass1(hp);
            ObjData.afteralloc;
 
@@ -1311,7 +1365,7 @@ Implementation
            ObjOutput.startobjectfile(ObjFileName);
            ObjData.resetsections;
            ObjData.beforewrite;
-           ObjData.createsection(startsectype,'');
+           ObjData.createsection(startsectype);
            hp:=TreePass2(hp);
            ObjData.afterwrite;
 

+ 3 - 1
compiler/dbgdwarf.pas

@@ -640,7 +640,9 @@ end;
                     include(def.defoptions,df_has_dwarf_dbg_info);
                   end
                 else
-                  current_asmdata.getdatalabel(TAsmLabel(def.dwarf_lab));
+                  { The pointer typecast is needed to prevent a problem with range checking
+                    on when the typecast is changed to 'as' }
+                  current_asmdata.getdatalabel(TAsmLabel(pointer(def.dwarf_lab)));
                 if def.dbg_state=dbg_state_used then
                   deftowritelist.Add(def);
               end;

+ 2 - 2
compiler/dbgstabs.pas

@@ -1496,7 +1496,7 @@ implementation
         { emit main source n_sourcefile for start of module }
         current_asmdata.getlabel(hlabel,alt_dbgfile);
         infile:=current_module.sourcefiles.get_file(1);
-        new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),0);
+        new_section(current_asmdata.asmlists[al_start],sec_code,make_mangledname('DEBUGSTART',current_module.localsymtable,''),0,secorder_begin);
         current_asmdata.asmlists[al_start].concat(tai_symbol.Createname_global(make_mangledname('DEBUGSTART',current_module.localsymtable,''),AT_DATA,0));
         if (infile.path^<>'') then
           current_asmdata.asmlists[al_start].concat(Tai_stab.Create_str(stab_stabs,'"'+BsToSlash(FixPath(infile.path^,false))+'",'+tostr(n_sourcefile)+
@@ -1506,7 +1506,7 @@ implementation
         current_asmdata.asmlists[al_start].concat(tai_label.create(hlabel));
         { emit empty n_sourcefile for end of module }
         current_asmdata.getlabel(hlabel,alt_dbgfile);
-        new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),0);
+        new_section(current_asmdata.asmlists[al_end],sec_code,make_mangledname('DEBUGEND',current_module.localsymtable,''),0,secorder_end);
         current_asmdata.asmlists[al_end].concat(tai_symbol.Createname_global(make_mangledname('DEBUGEND',current_module.localsymtable,''),AT_DATA,0));
         current_asmdata.asmlists[al_end].concat(Tai_stab.Create_str(stab_stabs,'"",'+tostr(n_sourcefile)+',0,0,'+hlabel.name));
         current_asmdata.asmlists[al_end].concat(tai_label.create(hlabel));

+ 29 - 25
compiler/ogbase.pas

@@ -202,8 +202,6 @@ interface
        procedure section_afteralloc(p:TObject;arg:pointer);
        procedure section_afterwrite(p:TObject;arg:pointer);
      protected
-       property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
-       property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
        property CObjSection:TObjSectionClass read FCObjSection write FCObjSection;
      public
        CurrPass  : byte;
@@ -211,10 +209,10 @@ interface
        constructor create(const n:string);virtual;
        destructor  destroy;override;
        { Sections }
-       function  sectionname(atype:TAsmSectiontype;const aname:string):string;virtual;
+       function  sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
        function  sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;virtual;
        function  sectiontype2align(atype:TAsmSectiontype):shortint;virtual;
-       function  createsection(atype:TAsmSectionType;const aname:string):TObjSection;
+       function  createsection(atype:TAsmSectionType;const aname:string='';aorder:TAsmSectionOrder=secorder_default):TObjSection;
        function  createsection(const aname:string;aalign:shortint;aoptions:TObjSectionOptions):TObjSection;virtual;
        procedure CreateDebugSections;virtual;
        function  findsection(const aname:string):TObjSection;
@@ -229,10 +227,8 @@ interface
        { Allocation }
        procedure alloc(len:aint);
        procedure allocalign(len:shortint);
-       procedure allocstab(p:pchar);
        procedure writebytes(const Data;len:aint);
        procedure writeReloc(Data,len:aint;p:TObjSymbol;Reloctype:TObjRelocationType);virtual;abstract;
-       procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);virtual;abstract;
        procedure beforealloc;virtual;
        procedure beforewrite;virtual;
        procedure afteralloc;virtual;
@@ -242,6 +238,8 @@ interface
        property CurrObjSec:TObjSection read FCurrObjSec;
        property ObjSymbolList:TFPHashObjectList read FObjSymbolList;
        property ObjSectionList:TFPHashObjectList read FObjSectionList;
+       property StabsSec:TObjSection read FStabsObjSec write FStabsObjSec;
+       property StabStrSec:TObjSection read FStabStrObjSec write FStabStrObjSec;
      end;
      TObjDataClass = class of TObjData;
 
@@ -770,7 +768,7 @@ implementation
       end;
 
 
-    function TObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
+    function TObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
       const
         secnames : array[TAsmSectiontype] of string[16] = ('',
           'code',
@@ -787,9 +785,21 @@ implementation
           'fpc',
           'toc'
         );
+      var
+        sep : string[3];
       begin
         if aname<>'' then
-          result:=secnames[atype]+'.'+aname
+          begin
+            case aorder of
+              secorder_begin :
+                sep:='.b_';
+              secorder_end :
+                sep:='.z_';
+              else
+                sep:='.n_';
+            end;
+            result:=secnames[atype]+sep+aname
+          end
         else
           result:=secnames[atype];
       end;
@@ -842,9 +852,9 @@ implementation
       end;
 
 
-    function TObjData.createsection(atype:TAsmSectionType;const aname:string):TObjSection;
+    function TObjData.createsection(atype:TAsmSectionType;const aname:string;aorder:TAsmSectionOrder):TObjSection;
       begin
-        result:=createsection(sectionname(atype,aname),sectiontype2align(atype),sectiontype2options(atype));
+        result:=createsection(sectionname(atype,aname,aorder),sectiontype2align(atype),sectiontype2options(atype));
       end;
 
 
@@ -981,16 +991,6 @@ implementation
       end;
 
 
-    procedure TObjData.allocstab(p:pchar);
-      begin
-        if not(assigned(FStabsObjSec) and assigned(FStabStrObjSec)) then
-          internalerror(200402254);
-        FStabsObjSec.alloc(sizeof(TObjStabEntry));
-        if assigned(p) and (p[0]<>#0) then
-          FStabStrObjSec.alloc(strlen(p)+1);
-      end;
-
-
     procedure TObjData.section_afteralloc(p:TObject;arg:pointer);
       begin
         with TObjSection(p) do
@@ -1033,13 +1033,17 @@ implementation
     procedure TObjData.beforewrite;
       var
         s : string[1];
+        hstab : TObjStabEntry;
       begin
         { create stabs sections if debugging }
         if assigned(StabsSec) then
          begin
-           writestab(0,nil,0,0,0,nil);
+           { Create dummy HdrSym stab, it will be overwritten in AfterWrite }
+           fillchar(hstab,sizeof(hstab),0);
+           StabsSec.Write(hstab,sizeof(hstab));
+           { start of stabstr }
            s:=#0;
-           stabstrsec.write(s[1],length(s));
+           StabStrSec.write(s[1],length(s));
          end;
       end;
 
@@ -1060,9 +1064,10 @@ implementation
           calculated more easily }
         if assigned(StabsSec) then
           begin
-            { header stab }
+            { end of stabstr }
             s:=#0;
-            stabstrsec.write(s[1],length(s));
+            StabStrSec.write(s[1],length(s));
+            { header stab }
             hstab.strpos:=1;
             hstab.ntype:=0;
             hstab.nother:=0;
@@ -1481,7 +1486,6 @@ implementation
         I1 : TObjSection absolute Item1;
         I2 : TObjSection absolute Item2;
       begin
-//writeln(I1.FullName);
         Result:=CompareStr(I1.FullName,I2.FullName);
       end;
 

+ 20 - 84
compiler/ogcoff.pas

@@ -129,9 +129,8 @@ interface
          constructor createcoff(const n:string;awin32:boolean;acObjSection:TObjSectionClass);
          destructor  destroy;override;
          procedure CreateDebugSections;override;
-         function  sectionname(atype:TAsmSectiontype;const aname:string):string;override;
+         function  sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
          procedure writereloc(data,len:aint;p:TObjSymbol;reloctype:TObjRelocationType);override;
-         procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
          procedure afteralloc;override;
        end;
 
@@ -914,9 +913,9 @@ const pemagic : array[0..3] of byte = (
         CObjSection:=ACObjSection;
         win32:=awin32;
         { we need at least the following 3 ObjSections }
-        createsection(sec_code,'');
-        createsection(sec_data,'');
-        createsection(sec_bss,'');
+        createsection(sec_code);
+        createsection(sec_data);
+        createsection(sec_bss);
       end;
 
 
@@ -926,14 +925,25 @@ const pemagic : array[0..3] of byte = (
       end;
 
 
-    function TCoffObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
+    function TCoffObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
       var
+        sep     : string[3];
         secname : string;
       begin
         secname:=coffsecnames[atype];
         if use_smartlink_section and
            (aname<>'') then
-          result:=secname+'.'+aname
+          begin
+            case aorder of
+              secorder_begin :
+                sep:='.b_';
+              secorder_end :
+                sep:='.z_';
+              else
+                sep:='.n_';
+            end;
+            result:=secname+sep+aname
+          end
         else
           result:=secname;
       end;
@@ -943,8 +953,8 @@ const pemagic : array[0..3] of byte = (
       begin
         if target_dbg.id=dbg_stabs then
           begin
-            stabssec:=createsection(sec_stab,'');
-            stabstrsec:=createsection(sec_stabstr,'');
+            stabssec:=createsection(sec_stab);
+            stabstrsec:=createsection(sec_stabstr);
           end;
       end;
 
@@ -1023,80 +1033,6 @@ const pemagic : array[0..3] of byte = (
       end;
 
 
-    procedure TCoffObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
-      const
-        N_SourceFile = $64;
-        N_IncludeFile = $84;
-      var
-        stab : TObjStabEntry;
-        stabstrlen : longint;
-        curraddr : aint;
-{$ifdef optimizestabs}
-        hs : string;
-{$endif optimizestabs}
-      begin
-        if not assigned(StabsSec) then
-          internalerror(200602256);
-        if assigned(p) and (p[0]<>#0) then
-          begin
-            stabstrlen:=strlen(p);
-{$ifdef optimizestabs}
-            StabStrEntry:=nil;
-            if (nidx=N_SourceFile) or (nidx=N_IncludeFile) then
-              begin
-                hs:=strpas(p);
-                StabstrEntry:=StabStrDict.Search(hs);
-                if not assigned(StabstrEntry) then
-                  begin
-                    StabstrEntry:=TStabStrEntry.Create(hs);
-                    StabstrEntry:=StabStrSec.Size;
-                    StabStrDict.Insert(StabstrEntry);
-                    { generate new stab }
-                    StabstrEntry:=nil;
-                  end;
-              end;
-            if assigned(StabstrEntry) then
-              stab.strpos:=StabstrEntry.strpos
-            else
-{$endif optimizestabs}
-              begin
-                stab.strpos:=StabStrSec.Size;
-                StabStrSec.write(p^,stabstrlen+1);
-              end;
-          end
-        else
-          stab.strpos:=0;
-        stab.ntype:=nidx;
-        stab.ndesc:=ndesc;
-        stab.nother:=nother;
-        stab.nvalue:=offset;
-        StabsSec.write(stab,sizeof(stab));
-        if assigned(ps) then
-         begin
-           { current address }
-           curraddr:=StabsSec.mempos+StabsSec.Size;
-	   if ps.bind=AB_LOCAL then
-	     begin
-               { avoid relocation in the .stab section
-                 because it ends up in the .reloc section instead }
-               if DLLSource and RelocSection then
-                 StabsSec.addsectionreloc(curraddr-4,ps.ObjSection,RELOC_RVA)
-               else
-                 StabsSec.addsectionreloc(curraddr-4,ps.ObjSection,RELOC_ABSOLUTE);
-	     end
-	   else
-	     begin
-               { avoid relocation in the .stab section
-                 because it ends up in the .reloc section instead }
-               if DLLSource and RelocSection then
-                 StabsSec.addsymreloc(curraddr-4,ps,RELOC_RVA)
-               else
-                 StabsSec.addsymreloc(curraddr-4,ps,RELOC_ABSOLUTE);
-	     end;     
-         end;
-      end;
-
-
     procedure TCoffObjData.afteralloc;
       var
         mempos : aint;
@@ -2388,7 +2324,7 @@ const pemagic : array[0..3] of byte = (
           with internalobjdata do
             begin
               secname:=basedllname+'_i_'+afuncname;
-              textobjsection:=createsection(sectionname(sec_code,secname),sectiontype2align(sec_code),sectiontype2options(sec_code) - [oso_keep]);
+              textobjsection:=createsection(sectionname(sec_code,secname,secorder_default),sectiontype2align(sec_code),sectiontype2options(sec_code) - [oso_keep]);
               idata4objsection:=createsection(sec_idata4, secname);
               idata5objsection:=createsection(sec_idata5, secname);
               idata6objsection:=createsection(sec_idata6, secname);

+ 22 - 45
compiler/ogelf.pas

@@ -55,10 +55,9 @@ interface
        public
          constructor create(const n:string);override;
          destructor  destroy;override;
-         function  sectionname(atype:TAsmSectiontype;const aname:string):string;override;
+         function  sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
          procedure CreateDebugSections;override;
          procedure writereloc(data,len:aint;p:TObjSymbol;reltype:TObjRelocationType);override;
-         procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
        end;
 
        TElfObjectOutput = class(tObjOutput)
@@ -307,13 +306,6 @@ implementation
           st_value : qword;
           st_size  : qword;
         end;
-        telf64stab=packed record
-          strpos  : longint;
-          ntype   : byte;
-          nother  : byte;
-          ndesc   : word;
-          nvalue  : longint;
-        end;
 
 
 {$ifdef cpu64bit}
@@ -558,14 +550,14 @@ implementation
         inherited create(n);
         CObjSection:=TElfObjSection;
         { we need at least the following sections }
-        createsection(sec_code,'');
-        createsection(sec_data,'');
-        createsection(sec_bss,'');
+        createsection(sec_code);
+        createsection(sec_data);
+        createsection(sec_bss);
         if tf_section_threadvars in target_info.flags then
-          createsection(sec_threadvar,'');
+          createsection(sec_threadvar);
         if (tf_needs_dwarf_cfi in target_info.flags) and
            (af_supports_dwarf in target_asm.flags) then
-             createsection(sec_debug_frame,'');
+             createsection(sec_debug_frame);
       end;
 
 
@@ -575,7 +567,7 @@ implementation
       end;
 
 
-    function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
+    function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
       const
         secnames : array[TAsmSectiontype] of string[13] = ('',
 {$ifdef userodata}
@@ -592,10 +584,22 @@ implementation
           'fpc',
           ''
         );
+      var
+        sep : string[3];
       begin
         if (use_smartlink_section and
            (aname<>'')) or (atype=sec_fpc) then
-          result:=secnames[atype]+'.'+aname
+          begin
+            case aorder of
+              secorder_begin :
+                sep:='.b_';
+              secorder_end :
+                sep:='.z_';
+              else
+                sep:='.n_';
+            end;
+            result:=secnames[atype]+sep+aname
+          end
         else
           result:=secnames[atype];
       end;
@@ -605,8 +609,8 @@ implementation
       begin
         if target_dbg.id=dbg_stabs then
           begin
-            stabssec:=createsection(sec_stab,'');
-            stabstrsec:=createsection(sec_stabstr,'');
+            stabssec:=createsection(sec_stab);
+            stabstrsec:=createsection(sec_stabstr);
           end;
       end;
 
@@ -653,33 +657,6 @@ implementation
       end;
 
 
-    procedure TElfObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
-      var
-        stab : TObjStabEntry;
-      begin
-        if not assigned(StabsSec) then
-          internalerror(200602271);
-        fillchar(stab,sizeof(TObjStabEntry),0);
-        if assigned(p) and (p[0]<>#0) then
-         begin
-           stab.strpos:=stabstrsec.Size;
-           stabstrsec.write(p^,strlen(p)+1);
-         end;
-        stab.ntype:=nidx;
-        stab.ndesc:=ndesc;
-        stab.nother:=nother;
-        stab.nvalue:=offset;
-        stabssec.write(stab,sizeof(stab));
-        if assigned(ps) then
-	  begin
-	    if ps.bind=AB_LOCAL then
-              stabssec.addsectionreloc(stabssec.Size-4,ps.Objsection,RELOC_ABSOLUTE)
-	    else  
-              stabssec.addsymreloc(stabssec.Size-4,ps,RELOC_ABSOLUTE);
-	  end;      
-      end;
-
-
 {****************************************************************************
                             TElfObjectOutput
 ****************************************************************************}