[build] Formalise mechanism for accessing absolute symbols

In a position-dependent executable, where all addresses are fixed
at link time, we can use the standard technique as documented by
GNU ld to get the value of an absolute symbol, e.g.:

    extern char _my_symbol[];

    printf ( "Absolute symbol value is %x\n", ( ( int ) _my_symbol ) );

This technique may not work in a position-independent executable.
When dynamic relocations are applied, the runtime addresses will no
longer be equal to the link-time addresses.  If the code to obtain the
address of _my_symbol uses PC-relative addressing, then it will
calculate the runtime "address" of the absolute symbol, which will no
longer be equal the the link-time "address" (i.e. the correct value)
of the absolute symbol.

Define macros ABS_SYMBOL(), ABS_VALUE_INIT(), and ABS_VALUE() that
provide access to the correct values of absolute symbols even in
position-independent code, and use these macros wherever absolute
symbols are accessed.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-05-09 14:25:59 +01:00
parent 1d58d928fe
commit 134d76379e
9 changed files with 112 additions and 20 deletions

View File

@@ -44,7 +44,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#undef CERT
#define CERT( _index, _path ) \
extern char stored_cert_ ## _index ## _data[]; \
extern char stored_cert_ ## _index ## _len[]; \
extern size_t ABS_SYMBOL ( stored_cert_ ## _index ## _len ); \
__asm__ ( ".section \".rodata\", \"a\", " PROGBITS "\n\t" \
"\nstored_cert_" #_index "_data:\n\t" \
".incbin \"" _path "\"\n\t" \
@@ -59,7 +59,7 @@ CERT_ALL
#undef CERT
#define CERT( _index, _path ) { \
.data = stored_cert_ ## _index ## _data, \
.len = ( size_t ) stored_cert_ ## _index ## _len, \
.len = ABS_VALUE_INIT ( stored_cert_ ## _index ## _len ), \
},
static struct asn1_cursor certstore_raw[] = {
CERT_ALL

View File

@@ -53,7 +53,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/* Raw private key data */
extern char private_key_data[];
extern char private_key_len[];
extern size_t ABS_SYMBOL ( private_key_len );
__asm__ ( ".section \".rodata\", \"a\", " PROGBITS "\n\t"
"\nprivate_key_data:\n\t"
#ifdef PRIVATE_KEY
@@ -68,7 +68,7 @@ struct private_key private_key = {
.refcnt = REF_INIT ( ref_no_free ),
.builder = {
.data = private_key_data,
.len = ( ( size_t ) private_key_len ),
.len = ABS_VALUE_INIT ( private_key_len ),
},
};