OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:07 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: UEFI in assembler
PostPosted: Mon May 03, 2021 10:30 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
zaval wrote:
That's why it's not PIC, because PIC means no references to absolute addresses at all, all references are relative, for example, to the program counter.
Well, in ELF, PIC just means that all accesses to memory happen through a separate pointer table (that gets fixed up by the loader). But I suppose the difference for PE would be rather small. It would just be whether the relocations affect code directly, or only data referenced by code.
Octocontrabass wrote:
PE does require RIP-relative addressing if the executable might be loaded above 2GB. That's probably the reason why PIC is always enabled for 64-bit Windows targets.
That and the fact that RIP-relative addressing makes PIC rather trivial for AMD64. But yes, I forgot you can only have four bytes offsets to most instructions.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: UEFI in assembler
PostPosted: Tue May 04, 2021 3:16 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
nullplan wrote:
Well, in ELF, PIC just means that all accesses to memory happen through a separate pointer table (that gets fixed up by the loader).

That's not entirely accurate. It is true that there are two tables, namely the PLT and the GOT but at least the PLT is not strictly required for PIC. The PLT is used for lazy binding and to implement that library calls can be intercepted by libraries that occur earlier in the load order (e.g., LD_PRELOAD lirbaries). You can build ELF DSOs that do not use a PLT by passing -fno-plt, but lazy binding will not work anymore if you do. The GOT (which is really just a plain data segment with no special handling outside of lazy binding) is required to load the same DSO to multiple locations: in the ELF model, you can share text and rodata segments between multiple instances of the same DSO. That is not possible in the PE model since PE modifies the text segment. The PE model is roughly equivalent to ELF object files (linked together with -r; for example, this strategy is used to build Linux kernel modules).

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: UEFI in assembler
PostPosted: Tue May 04, 2021 12:51 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
Korona wrote:
That's not entirely accurate.
So where was any of what I wrote inaccurate? I'm still waiting. You expanded on my paraphrase, and gave the proper nouns for the table I mentioned, and also mentioned the PLT for some odd reason. But you never showed any of what I wrote to be wrong or incomplete.

The PLT only exists to unify the calling sequences, to allow non-PIC code to be linked with PIC code, and to group the code pointers in the GOT together (to facilitate lazy binding). But heart and soul of ELF PIC is the GOT. There is one exception I know of, and it is the original PowerPC ABI, the one with the BSS PLT. What were they thinking? But that ABI is no longer in use, and for example musl doesn't support it. In all other ABIs, the PLT stub will merely jump to the function through the GOT pointer.

Not that any of this has any bearing on the original subject. The GOT is only needed for external symbols, and there are no external symbols in UEFI (the system table contains function pointers instead), and UEFI uses PE instead of ELF. UEFI doesn't even exist for PowerPC!

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: UEFI in assembler
PostPosted: Tue May 04, 2021 1:42 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
nullplan wrote:
So where was any of what I wrote inaccurate? I'm still waiting.

I guess it was also not entirely inaccurate; but it's not the full story in the sense that ELF doesn't require all accesses to go through a table -- you can build ELFs that do not use indirection tables: namely, if you do not require lazy binding and you do not require the DSO to share its text segment with multiple processes. The ELF model supports a superset of the features of PE (regarding PIC). I brought up the PLT because out of the two tables, the PLT is the one that is special-cased by the linker and that is responsible for indirection -- I thought you meant the PLT when you stated "indirection table". The GOT is merely a part of the normal data segment (except for the first two entries, which handle the PLT).

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: UEFI in assembler
PostPosted: Tue May 04, 2021 2:50 pm 
Offline
Member
Member
User avatar

Joined: Fri Feb 17, 2017 4:01 pm
Posts: 640
Location: Ukraine, Bachmut
nullplan wrote:
Well, in ELF, PIC just means that all accesses to memory happen through a separate pointer table (that gets fixed up by the loader). But I suppose the difference for PE would be rather small. It would just be whether the relocations affect code directly, or only data referenced by code.

I thought, if code and data contains absolute addresses, it's called Position Dependent Code. That's what it is in PE. If it's PIC as well, then what is non PIC? :)

Octocontrabass wrote:
PE does require RIP-relative addressing if the executable might be loaded above 2GB. That's probably the reason why PIC is always enabled for 64-bit Windows targets.

I am lazy to check if there are x64 base relocation types in the PE spec, but I just looked at my loader, full of global variables and indeed - for the x64 and arm64 images, Base Relocation Directory is empty, where it's not empty for arm32. So, at least specifics of architectures, make images truely PIC. Of course, ELF PIC is quazi PIC, since it needs fixing anyway. Just special "indirection" tables, rather the code/data as in PE.

_________________
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).


Top
 Profile  
 
 Post subject: Re: UEFI in assembler
PostPosted: Tue May 04, 2021 3:15 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
zaval wrote:
I am lazy to check if there are x86 base relocation types in the PE spec, but I just looked at my loader, full of global variables and indeed - for the x64 and arm64 images, Base Relocation Directory is empty, where it's not empty for arm32.

There can still be relocation entries. For example, initialize one of your global variables with a function pointer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 60 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group