Recent

Author Topic: Strange Error when I use my self-crafted linker to link to the ELF executable  (Read 884 times)

TYDQ

  • Full Member
  • ***
  • Posts: 153
These days I am trying to craft my Linker(pre-release version of the Linker) unild for linking the compiled object files to the executable in Linux.
However,I encountered some errors when I use my DIY linker to link the compiled and assembled object files to the executable and then execute it.I have coding a minimal error project to reproduce the error I have met(Segmentation fault).
The details of the error was(which GNU Debugger(GDB) displays):
Code: Text  [Select][+][-]
  1. Program received signal SIGSEGV, Segmentation fault.
  2. SYSTEM_$$_init$ () at system.pp:691
  3. 691     begin
  4. (gdb) bt
  5. #0  SYSTEM_$$_init$ () at system.pp:691
  6. #1  0x0000000000416b37 in fpc_initializeunits () at ../inc/system.inc:1170
  7. #2  0x0000000000400143 in main () at example.pas:5
Both the unild source code and test code attached on the attachments,The testing code is:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE ObjFPC}{$H+}
  4.  
  5. begin
  6.  writeln('This is a Hello World Program!');
  7. end.
The Attachment errorproject.zip consists two folder unild and testexample.
Please install the FPC trunk version to compile these two projects.
After the FPC trunk on ready,You must bash build.sh in the unild folder and then bash build.sh in the testexample folder,the second step that reproduce the Segmentation Fault(Must be Linux,cannot test the error on Windows and other due to the unild cannot recognize non-ELF object files).

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12637
  • FPC developer.
Apparently your linker does something wrong.  IIRC Initializeunit uses a table with the offsets of the various initialization sections of units. Maybe that is not correctly linked/relocated whatever.

I'd try to debug with gdb (not only when it crashes, but step for step), maybe also use objdump

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
For your code to work, you can not (easily) skip system.pas although you can rewrite that to some extend: the compiler has lots of "compiler magic" that you can not easily skip. That is opposed to, say, a C compiler. E.g. input/output is generated by the compiler, NOT a library.
Your code *must* at least solve but not necessarily implement other than stubs , references to all compiler magic. These are listed in *.fpg files in the compiler sources: these are references for which the compiler does not need library code but generated by the compiler itself.
« Last Edit: January 21, 2026, 11:12:33 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Marco, can you ping if the fpg extension is indeed correct? Can't verify atm.

(I have analyzed his code, hence my reply above)
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TYDQ

  • Full Member
  • ***
  • Posts: 153
Apparently your linker does something wrong.  IIRC Initializeunit uses a table with the offsets of the various initialization sections of units. Maybe that is not correctly linked/relocated whatever.

I'd try to debug with gdb (not only when it crashes, but step for step), maybe also use objdump
Pardon my ignorance, what is the IIRC initalizeunit,is it fpc_initializeunit?
(Using GNU ld can link it and execute it correctly,while my linker can link but linked executable will throw a Segmentation Error.)

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Pardon my ignorance, what is the IIRC initalizeunit,is it fpc_initializeunit?
(Using GNU ld can link it and execute it correctly,while my linker can link but linked executable will throw a Segmentation Error.)
Are you sure? I tried that with the gold linker and that fails as well.
There are references to non-existing code, hence it SIGSEVs. (Even if it can never be executed)

Maybe a standard (old) ld is more forgiving regarding non-existing entry points.
Focus on the latter, if you are writing your own linker, you know where to look.

[edit]
You may also experiment with some of the -X options and/or -s (lowercase) options to try and get your linker to work/debug. These are normally not relevant, but affect linker behavior.
« Last Edit: January 21, 2026, 11:33:13 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TYDQ

  • Full Member
  • ***
  • Posts: 153
Pardon my ignorance, what is the IIRC initalizeunit,is it fpc_initializeunit?
(Using GNU ld can link it and execute it correctly,while my linker can link but linked executable will throw a Segmentation Error.)
Are you sure? I tried that with the gold linker and that fails as well.
There are references to non-existing code, hence it SIGSEVs. (Even if it can never be executed)

Maybe a standard (old) ld is more forgiving regarding non-existing entry points.
Focus on the latter, if you are writing your own linker, you know where to look.

[edit]
You may also experiment with some of the -X options and/or -s (lowercase) options to try and get your linker to work/debug)
Thanks a lot for your direct solution.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Did it work or not? I have the code open and want to help you...
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TYDQ

  • Full Member
  • ***
  • Posts: 153
Did it work or not? I have the code open and want to help you...
I will consuming some time to check it,maybe some hours to do it.

TYDQ

  • Full Member
  • ***
  • Posts: 153
Did it work or not? I have the code open and want to help you...
Unforunately,it didn't.I am adding some vaildation code for my hash table to vaildate the relocation existence and effectiveness but didn't vaildate any error about it.
As you view the code,which non-exist references in relocation my linker have linked,can you partly list it?
An Attachment that Linker Script for unild don't omit any section that tested but failed to execute in Linux (My Operating System is Fedora Linux 43),you can test that.If you have any questions about my code of unild,You can reply then.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
OK. I am currently not at the PC we started the conversation with due to urgent matters, but will be back later.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TYDQ

  • Full Member
  • ***
  • Posts: 153
Apparently your linker does something wrong.  IIRC Initializeunit uses a table with the offsets of the various initialization sections of units. Maybe that is not correctly linked/relocated whatever.

I'd try to debug with gdb (not only when it crashes, but step for step), maybe also use objdump
Buddy,what error you got from the GDB and objdump?

TYDQ

  • Full Member
  • ***
  • Posts: 153
Apparently your linker does something wrong.  IIRC Initializeunit uses a table with the offsets of the various initialization sections of units. Maybe that is not correctly linked/relocated whatever.

I'd try to debug with gdb (not only when it crashes, but step for step), maybe also use objdump
Do you have any progress about debugging my executable and find the point that why my program occurs error?
I'm confused and frustrated with this error but have no solution to address this problem.
« Last Edit: January 22, 2026, 03:16:27 pm by TYDQ »

TYDQ

  • Full Member
  • ***
  • Posts: 153
This error is solved,due to program on linux must be 4KiB aligned can be executed otherwise you will got an SIGSEGV Error.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 380
  • I use FPC [main] 💪🐯💪
I wonder if it is possible to devise something for FPC so that it optimizes in a manner similar to LD/LLD "Identical Code Folding (ICF)"

This may be useful for code with generics
« Last Edit: January 25, 2026, 04:11:56 am by ALLIGATOR »
I may seem rude - please don't take it personally

 

TinyPortal © 2005-2018