OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:33 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [solved] clang, uefi and global constructors
PostPosted: Mon Aug 30, 2021 7:04 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
I am using clang to cross-compile a UEFI bootloader (I use "-target x86_64-unknown-windows"). I would like to call global constructors before running any other code. I am not able to find the usual suspects ".init_array", "__CTOR_LIST__", "__init()" etc. Of course I am not linking any of the crtxxx stuff as this is a "bare metal" target.

Dumping the PDB I can see that there are references to global constructors (so they are generated):

Code:
  0x1006 | LF_FUNC_ID [size = 48, hash = 0x38C6C]
           name = `dynamic initializer for 'g_test', type = 0x102E, parent scope = <no type>
  0x10B3 | LF_FUNC_ID [size = 52, hash = 0x2B878]
           name = metal::`dynamic initializer for 'g_log', type = 0x102E, parent scope = <no type>

    5976 | S_LPROCREF [size = 48] ``dynamic initializer for 'g_test'`
           module = 1, sum name = 0, offset = 236
   11592 | S_LPROCREF [size = 56] `metal::`dynamic initializer for 'g_log'`
           module = 11, sum name = 0, offset = 52

    6244 | S_GDATA32 [size = 24] `g_test`
           type = 0x1033 (Test), addr = 0003:0032
   11916 | S_GDATA32 [size = 28] `metal::g_log`
           type = 0x119E (metal::LogSystem), addr = 0003:1032

Dumping the PE file, I see a ".CRT" section that might or might not be related. It basically contains some address that points within the code, but I am not sure what is going on here.

What I cannot find is a way to run them on entry. The googles are not working today. Any idea?

_________________
https://github.com/kiznit/rainbow-os


Last edited by kzinti on Mon Aug 30, 2021 8:01 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: clang, uefi and global constructors
PostPosted: Mon Aug 30, 2021 7:32 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
It does look like the constructors end up listed in a table in the ".CRT" section. I have two global constructors and my ".CRT" is 16 bytes long. It contains two 64-bits addresses that point to initialization code for them.

At this point the question is how to I get access to that ".CRT" section from the PE code itself. I am thinking that adding two symbols in the .CRT section (at the beginning and end of the array) should do. That seems to be what VC does: https://docs.microsoft.com/en-us/cpp/c- ... om=vs-2019

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: [solved] clang, uefi and global constructors
PostPosted: Mon Aug 30, 2021 8:01 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Found a working solution here: https://docs.microsoft.com/en-us/cpp/pr ... w=msvc-160

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot], SemrushBot [Bot] and 53 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