Compiling with PIE on Linux

Linux specific questions.
Post Reply
jakobssystems
Posts: 36
Joined: Aug 11, 2024 8:06

Compiling with PIE on Linux

Post by jakobssystems »

I have a small app for both Windows and Linux and seperated code for these platforms with compiler directives:

Code: Select all

	#IF DEFINED(__FB_WIN32__)
		'Windows related only
	#ELSEIF DEFINED(__FB_LINUX__)
		'Linux stuff here
	#ENDIF
When I try to compile with PIE I am getting error message from the Code inside FB_WIN32 Block which is of course wrong because I am using static pointers in there. (PIE is Linux only).

Code: Select all

xxx.bas(29) warning 3(2): Passing different pointer types, at parameter 1 of LSTRCPYW()
ld: /usr/lib/gcc/x86_64-linux-gnu/14/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a PIE object
ld: failed to set dynamic section sizes: bad value
What I am missing? Without PIE everything compiles and runs fine. though dpkg linter gives "hardening-no-pie" warnings.
it looks like a bug, the compiler should ignore the Windows related part.

this is my compiler command:

Code: Select all

fbc -Wc -fPIC -Wl -pie -O 3 -s gui -strip -exx -w all $FILENAME.bas
jakobssystems
Posts: 36
Joined: Aug 11, 2024 8:06

Re: Compiling with PIE on Linux

Post by jakobssystems »

I have even tried to seperate the culpit function like that:

Code: Select all

#IF DEFINED(__FB_WIN32__)
	#Include "win_ReadClipboard.bas"
#ELSEIF DEFINED(__FB_LINUX__)
	#Include "linux_ReadClipboard.bas"
#ENDIF
w/o success. the compiler ignored the #IF and throws an error:

Code: Select all

in_ReadClipboard.bas(15) warning 3(2): Passing different pointer types, at parameter 1 of LSTRCPYW()
it shouldn't do this!
caseih
Posts: 2168
Joined: Feb 26, 2007 5:32

Re: Compiling with PIE on Linux

Post by caseih »

I created a simple test program:

Code: Select all

#IF DEFINED(__FB_WIN32__)
	syntax error
#ELSEIF DEFINED(__FB_LINUX__)
	'Linux stuff here
	print "Linux here."
#ENDIF
When I compile with those flags I get:

Code: Select all

$ /opt/FreeBASIC/bin/fbc -Wc -fPIC -Wl -pie -O 3 -s gui -strip -exx -w all pietest.bas
ld: /usr/lib/gcc/x86_64-redhat-linux/14/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a PIE object
ld: failed to set dynamic section sizes: bad value
But the code in the windows-specific section definitely gets excluded (or else I'd get a syntax error).
jakobssystems
Posts: 36
Joined: Aug 11, 2024 8:06

Re: Compiling with PIE on Linux

Post by jakobssystems »

good morning, my code is here:
https://codeberg.org/tomas-jakobs/clipb ... uditor.bas

Normal compile w/o PIE says:

Code: Select all

clipboard-auditor.bas(29) warning 3(2): Passing different pointer types, at parameter 1 of LSTRCPYW()
which seems clear because I used any ptr in the windows block, but at least it compiles.

Compile with PIE stops compiling

Code: Select all

clipboard-auditor.bas(29) warning 3(2): Passing different pointer types, at parameter 1 of LSTRCPYW()
ld: /usr/lib/gcc/x86_64-linux-gnu/14/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a PIE object
ld: failed to set dynamic section sizes: bad value

For me this is a compiler error and/or I cannot strip when compiling with PIE?
I am not familiar with compilers and how they internally work, but it seems, that stripping debug infos does not work with compiling in PIE for linux, does it?
Nevertheless the warning inside Windows block shouldn't be thrown when compiling for Linux, this is clearly an error.

My compile script for both win and linux is here:
https://codeberg.org/tomas-jakobs/clipb ... osscompile
RetardedAndProud
Posts: 7
Joined: Jun 09, 2024 18:26

Re: Compiling with PIE on Linux

Post by RetardedAndProud »

jakobssystems wrote: Nov 20, 2024 20:46

Code: Select all

fbc -Wc -fPIC -Wl -pie -O 3 -s gui -strip -exx -w all $FILENAME.bas
Add -pic to fbc options...

Code: Select all

fbc -pic -Wc -fPIC -Wl -pie -O 3 -s gui -strip -exx -w all $FILENAME.bas
Worked for me :D
jakobssystems
Posts: 36
Joined: Aug 11, 2024 8:06

Re: Compiling with PIE on Linux

Post by jakobssystems »

Yes, this worked for me now too, thank you a lot!
Post Reply