From: Indu Bhagat Date: Mon, 26 May 2025 22:06:31 +0000 (-0700) Subject: bfd: gas: ld: libsframe: adopt new encoding for FDE func start addr field X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c89f42b72f4ae4f0de0c4a37013a465c97e13cf1;p=thirdparty%2Fbinutils-gdb.git bfd: gas: ld: libsframe: adopt new encoding for FDE func start addr field This patch convenes a set of changes in bfd, gas, ld, libsframe towards moving to the new encoding for the 'sfde_func_start_address' field in SFrame FDE. First, gas must now mark all SFrame sections with the new flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL. gas was already emitting the field in the said encoding. * gas/gen-sframe.c (output_sframe_internal): Emit the flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL. Similarly for ld, adopt the new semantics of sfde_func_start_address consistently. This means: - When merging SFrame sections, check that all input SFrame sections have the SFRAME_F_FDE_FUNC_START_ADDR_PCREL flag set. If the check fails, ld errors out. - When merging SFrame sections, keep even the in-memory contents of the FDE function start address (buffer passed to libsframe sframe_encoder_write () for writing out) are encoded in the new semantics. While it is, in theory, possible that instead of doing this change here, we adjust the value of sfde_func_start_address at the final write (sframe_encoder_write) time. But latter is not favorable for maintenanance and may be generally confusing for developers. - When creating SFrame for PLT entries, emit flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL. include/ * sframe-api.h (SFRAME_F_LD_MUSTHAVE_FLAGS): New definition. bfd/ * elf-sframe.c (_bfd_elf_merge_section_sframe): Check for flag combinatation SFRAME_F_LD_MUSTHAVE_FLAGS set for all input and output SFrame sections. If not, error out. Also, adopt the new semantics of function start address encoding. * bfd/elfxx-x86.c (_bfd_x86_elf_create_sframe_plt): Emit flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL. Next, for dumping SFrame sections, now that we are emitting the same encoding in GAS, non-relocatable and relocatable SFrame links, it is the time to set relocate to TRUE in debug_displays[]. binutils/ * dwarf.c (struct dwarf_section_display): Allow sframe sections to now be relocated. gas/testsuite/ * gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d: Update the test. Relocatable SFrame sections now display non-zero value (appropriate function start address). Now, as the SFrame sections on-disk and in-memory use the new semantics of sfde_func_start_address encoding (i.e., function start address is the offset from the sfde_func_start_address field to the start PC), the calculation to make it human readable (i.e., relatable to the addresses in .text sections) needs adjustment. libsframe/ * sframe-dump.c (dump_sframe_func_with_fres): Adjust the function start address for dumping. Now that both the emission of the new encoding, and the relocation of sections before dumping them is in place, it is time to adjust the testcases. gas/testsuite/ * gas/cfi-sframe/cfi-sframe-aarch64-1.d: Update expected output to include SFRAME_F_FDE_FUNC_START_ADDR_PCREL instead of NONE. * gas/cfi-sframe/cfi-sframe-aarch64-2.d: Likewise. * gas/cfi-sframe/cfi-sframe-aarch64-3.d: Likewise. * gas/cfi-sframe/cfi-sframe-aarch64-4.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-1.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-10.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-11.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-2.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-3.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-4.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-5.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-6.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-7.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-8.d: Likewise. * gas/cfi-sframe/cfi-sframe-common-9.d: Likewise. * gas/cfi-sframe/cfi-sframe-x86_64-1.d: Likewise. * gas/cfi-sframe/cfi-sframe-x86_64-2.d: Likewise. * gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d: Likewise. * gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d: Likewise. * gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d: Likewise. * gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d: Likewise. * gas/cfi-sframe/common-empty-1.d: Likewise. * gas/cfi-sframe/common-empty-2.d: Likewise. * gas/cfi-sframe/common-empty-3.d: Likewise. * gas/scfi/x86_64/scfi-cfi-sections-1.d: Likewise. * gas/scfi/x86_64/scfi-dyn-stack-1.d: Likewise. ld/testsuite/ * ld-aarch64/sframe-simple-1.d: Update expected output to include SFRAME_F_FDE_FUNC_START_ADDR_PCREL. * ld-x86-64/sframe-ibt-plt-1.d: Likewise. * ld-x86-64/sframe-plt-1.d: Likewise. * ld-x86-64/sframe-pltgot-1.d: Likewise. * ld-x86-64/sframe-pltgot-2.d: Likewise. * ld-x86-64/sframe-simple-1.d: Likewise. Naturally, the change of semantics for 'SFrame FDE function start address' has consequences on the implementation in libsframe. As per the new semantics: - Function start address in the SFrame FDE (sfde_func_start_address) is an offset from the FDE function start address field to the start PC of the associated function. Note that, the libsframe library brings the SFrame section contents into its own memory to create a sframe_decoder_ctx object via sframe_decode (). Many internal and user-interfacing APIs then use sframe_decoder_ctx object to interact and fulfill the work. In context of changing semantics for sfde_func_start_address, following relevant examples may help understand the impact: - sframe_find_fre () finds a the SFrame stack trace data (SFrame FRE) given a lookup offset (offset of lookup_pc from the start of SFrame section). Now that the sfde_func_start_address includes the distance from the sfde_func_start_address field to the start of SFrame section itself, the comparison checks of sfde_func_start_address with the incoming lookup offset need adjustment. - Some internal functions (sframe_get_funcdesc_with_addr_internal () finds SFrame FDE by using binary seach comparing sfde_func_start_address fields, etc.) need adjustments. - sframe_encoder_write () sorts the SFrame FDEs before writing out the SFrame data. Sorting of SFrame FDE via the internal function sframe_sort_funcdesc() needs adjustments: the new encoding of sfde_func_start_address means the distances are not from the same anchor, so cannot be sorted directly. This patch takes the approach of adding a new internal function: - sframe_decoder_get_secrel_func_start_addr (): This function returns the offset of the start PC of the function from the start of SFrame section, i.e., it gives a section-relative offset. As the sframe_decoder_get_secrel_func_start_addr () API needs the value of the function index in the FDE list, another internal API needs sframe_fre_check_range_p () adjustments too. Sorting the FDEs (via sframe_sort_funcdesc ()) is done by first bringing all offsets in sfde_func_start_address relative to start of SFrame section, followed by sorting, and then readjusting the offsets accroding to the new position in the FDE list. libsframe/ * sframe.c (sframe_decoder_get_secrel_func_start_addr): New static function. (sframe_fre_check_range_p): Adjust the interface a bit. (sframe_get_funcdesc_with_addr_internal): Use sframe_decoder_get_secrel_func_start_addr () when comparing sfde_func_start_address with user input offset. (sframe_find_fre): Adopt the new semantics. (sframe_sort_funcdesc): Likewise. For the libsframe testsuite, use the new encoding for FDE func start addr: distance between the FDE sfde_func_start_address field and the start PC of the function itself. Use SFRAME_F_FDE_FUNC_START_ADDR_PCREL flag, though the sframe_encode () interface in libsframe applies no sanity checks for the encoding itself. libsframe/testsuite/ * libsframe.find/findfre-1.c: Adjust to use the new SFRAME_F_FDE_FUNC_START_ADDR_PCREL specific encoding. * libsframe.find/findfunc-1.c: Likewise. * libsframe.find/plt-findfre-1.c: Likewise. * libsframe/testsuite/libsframe.decode/DATA2: Update data file due to usage of new SFRAME_F_FDE_FUNC_START_ADDR_PCREL flag. * libsframe/testsuite/libsframe.encode/encode-1.c: Use flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL. --- [Changes in V4] - Define new SFRAME_F_LD_MUSTHAVE_FLAGS. This will be the set of flags that must be the same for all input SFrame decoder objects and output SFrame encoder object. - Use condensed form of check "(dctx_flags ^ ectx_flags) & SFRAME_F_LD_MUSTHAVE_FLAGS" to enforce that decoder and encoder flags are harmonious. - Update commit log to weave in SFRAME_F_LD_MUSTHAVE_FLAGS. - Add explicit check in sframe_encode () for the flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL to be set. The write APIs of the SFrame encoder object are doing the right thing only for the case when SFRAME_F_FDE_FUNC_START_ADDR_PCREL is set. - Explicitly set SFRAME_F_FDE_FUNC_START_ADDR_PCREL before call to sframe_encode () in _bfd_elf_merge_section_sframe (). - Fix encode-1.c testcase to use SFRAME_F_FDE_FUNC_START_ADDR_PCREL flag. This was uncovered due to the changes related to usage of SFRAME_F_FDE_FUNC_START_ADDR_PCREL for invocation of and inside sframe_encode (). - Update code comments on why special handling of SFRAME_F_FDE_SORTED before sframe_encode () call in _bfd_elf_merge_section_sframe (). - Removed the TBD for libsframe versioning. The plan is to create a LIBSFRAME_2.0 { global: sframe_decoder_get_flags; sframe_decoder_get_offsetof_fde_start_addr; sframe_encoder_get_flags; sframe_encoder_get_offsetof_fde_start_addr; ... // list all the APIs. }; before the 2.45 release. [End of changes in V4] [No changes in V3] [No changes in V2] --- diff --git a/bfd/elf-sframe.c b/bfd/elf-sframe.c index 97e00734515..7a79ff78433 100644 --- a/bfd/elf-sframe.c +++ b/bfd/elf-sframe.c @@ -328,6 +328,8 @@ _bfd_elf_merge_section_sframe (bfd *abfd, int8_t sfd_ctx_fixed_ra_offset; uint8_t dctx_version; uint8_t ectx_version; + uint8_t dctx_flags; + uint8_t ectx_flags; int encerr = 0; struct elf_link_hash_table *htab; @@ -351,6 +353,8 @@ _bfd_elf_merge_section_sframe (bfd *abfd, if (sfd_ctx == NULL || sfe_info == NULL) return false; + dctx_flags = sframe_decoder_get_flags (sfd_ctx); + if (htab->sfe_info.sfe_ctx == NULL) { sfd_ctx_abi_arch = sframe_decoder_get_abi_arch (sfd_ctx); @@ -361,8 +365,18 @@ _bfd_elf_merge_section_sframe (bfd *abfd, if (!sfd_ctx_abi_arch) return false; + /* In-memory FDEs in the encoder object are unsorted during linking and + will be sorted before emission. Reset SFRAME_F_FDE_SORTED to aptly + reflect that (doing so has no other functional value at this time + though). */ + uint8_t tflags = dctx_flags & ~SFRAME_F_FDE_SORTED; + /* ld always generates an output section with + SFRAME_F_FDE_FUNC_START_ADDR_PCREL flag set. Later using + SFRAME_F_LD_MUSTHAVE_FLAGS, it is enforced that the provided input + sections also have this flag set. */ + tflags |= SFRAME_F_FDE_FUNC_START_ADDR_PCREL; htab->sfe_info.sfe_ctx = sframe_encode (SFRAME_VERSION_2, - 0, /* SFrame flags. */ + tflags, /* SFrame flags. */ sfd_ctx_abi_arch, sfd_ctx_fixed_fp_offset, sfd_ctx_fixed_ra_offset, @@ -410,7 +424,15 @@ _bfd_elf_merge_section_sframe (bfd *abfd, " .sframe generation")); return false; } - + /* Check that all SFrame sections being linked have harmonious flags. */ + ectx_flags = sframe_encoder_get_flags (sfe_ctx); + if ((dctx_flags ^ ectx_flags) & SFRAME_F_LD_MUSTHAVE_FLAGS) + { + _bfd_error_handler + (_("input SFrame sections with different FDE encoding prevent" + " .sframe generation")); + return false; + } /* Iterate over the function descriptor entries and the FREs of the function from the decoder context. Add each of them to the encoder @@ -431,6 +453,7 @@ _bfd_elf_merge_section_sframe (bfd *abfd, bool pltn_reloc_by_hand = false; unsigned int pltn_r_offset = 0; uint8_t rep_block_size = 0; + uint32_t offsetof_fde_in_sec = 0; if (!sframe_decoder_get_funcdesc_v2 (sfd_ctx, i, &num_fres, &func_size, &func_start_addr, &func_info, @@ -481,6 +504,15 @@ _bfd_elf_merge_section_sframe (bfd *abfd, address += sframe_read_value (abfd, contents, pltn_r_offset, 4); address += (sec->output_offset + r_offset); + /* SFrame FDE function start address is an offset from the + sfde_func_start_address field to the start PC. The + calculation below is the distance of sfde_func_start_address + field from the start of the output SFrame section. */ + offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (sfe_ctx, + cur_fidx + num_enc_fidx, + NULL); + address -= offsetof_fde_in_sec; /* FIXME For testing only. Cleanup later. */ // address += (sec->output_section->vma); diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c index bc9bb7010ce..d55eecdcceb 100644 --- a/bfd/elfxx-x86.c +++ b/bfd/elfxx-x86.c @@ -1891,7 +1891,7 @@ _bfd_x86_elf_create_sframe_plt (bfd *output_bfd, } *ectx = sframe_encode (SFRAME_VERSION_2, - 0, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL, SFRAME_ABI_AMD64_ENDIAN_LITTLE, SFRAME_CFA_FIXED_FP_INVALID, -8, /* Fixed RA offset. */ diff --git a/binutils/dwarf.c b/binutils/dwarf.c index 29fbf541149..fe51f015901 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -12863,7 +12863,7 @@ struct dwarf_section_display debug_displays[] = { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false }, { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false }, { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false }, - { { ".sframe", "", "", NO_ABBREVS }, display_sframe, &do_sframe, false }, + { { ".sframe", "", "", NO_ABBREVS }, display_sframe, &do_sframe, true }, { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true }, { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false }, { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false }, diff --git a/gas/gen-sframe.c b/gas/gen-sframe.c index 2d1c1961fd7..0137ea600a6 100644 --- a/gas/gen-sframe.c +++ b/gas/gen-sframe.c @@ -665,6 +665,10 @@ output_sframe_internal (void) /* The function descriptor entries as dumped by the assembler are not sorted on PCs. */ unsigned char sframe_flags = 0; + /* Fix PR ld/32666 - Incorrect .rela.sframe when using ld -r. + With the fix now in place, we indicate the new encoding with an additional + flag in SFrame Version 2. */ + sframe_flags |= SFRAME_F_FDE_FUNC_START_ADDR_PCREL; unsigned int num_fdes = get_num_sframe_fdes (); unsigned int num_fres = get_num_sframe_fres (); diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-1.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-1.d index 8ae46217117..194a9fbba51 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-1.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-1.d @@ -6,7 +6,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL Num FDEs: 1 Num FREs: 3 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-2.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-2.d index b7834d53b07..45e64dc4248 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-2.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-2.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL Num FDEs: 1 Num FREs: 2 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-3.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-3.d index f72b70a970a..7a7fa5e2926 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-3.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-3.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL Num FDEs: 1 Num FREs: 2 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-4.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-4.d index 51fad9c0893..73433a0a9b9 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-4.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-4.d @@ -16,7 +16,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL Num FDEs: 0 Num FREs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d index 599d4c4e795..5d676bd312a 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL Num FDEs: 2 Num FREs: 6 @@ -18,10 +18,10 @@ Contents of the SFrame section .sframe: 0+0004 +sp\+0 +u +u\[s\] + 0+0008 +sp\+16 +c-16 +c-8\[s\] + - func idx \[1\]: pc = 0x0, size = 20 bytes, pauth = B key + func idx \[1\]: pc = 0xc, size = 20 bytes, pauth = B key STARTPC + CFA + FP + RA + - 0+0000 +sp\+0 +u +u + - 0+0004 +sp\+0 +u +u\[s\] + - 0+0008 +sp\+16 +c-16 +c-8\[s\] + + 0+000c +sp\+0 +u +u + + 0+0010 +sp\+0 +u +u\[s\] + + 0+0014 +sp\+16 +c-16 +c-8\[s\] + #pass diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-1.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-1.d index 5f4ae00747d..676641fa574 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-1.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-1.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-10.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-10.d index 106e05d160b..737ceba1aa1 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-10.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-10.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-11.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-11.d index 25848150135..bca05d4d191 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-11.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-11.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-2.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-2.d index ded8c450a94..bf142e1fe32 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-2.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-2.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-3.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-3.d index d23fd9790f6..d69fc3758fe 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-3.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-3.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-4.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-4.d index 8632613f532..7be3cafb259 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-4.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-4.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-5.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-5.d index dd2c32d3d9f..b3fe0c15499 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-5.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-5.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-6.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-6.d index 34390c46a07..30824b05eea 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-6.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-6.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-7.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-7.d index 61efb9c4ed1..bac4d46d30a 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-7.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-7.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-8.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-8.d index d77645636b3..ec3afb6f6bd 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-8.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-8.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-9.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-9.d index 80c92357073..877dfafaae9 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-9.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-common-9.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-1.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-1.d index 88b4cc63dba..3d8e93619d1 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-1.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-1.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 Num FDEs: 1 Num FREs: 4 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-2.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-2.d index d779b6d126e..44210368e04 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-2.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-2.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 1 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d index 0bae4b95b31..1cf47aae365 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d index f1ba0443273..24b7e2ae0b5 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d index c8fc7a43dd4..4ad3efbb1e1 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d index 43f09e09d27..2e746147b24 100644 --- a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d +++ b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/common-empty-1.d b/gas/testsuite/gas/cfi-sframe/common-empty-1.d index 736aea87505..ee351b05823 100644 --- a/gas/testsuite/gas/cfi-sframe/common-empty-1.d +++ b/gas/testsuite/gas/cfi-sframe/common-empty-1.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/common-empty-2.d b/gas/testsuite/gas/cfi-sframe/common-empty-2.d index 163b504a687..f268bcf0d76 100644 --- a/gas/testsuite/gas/cfi-sframe/common-empty-2.d +++ b/gas/testsuite/gas/cfi-sframe/common-empty-2.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/cfi-sframe/common-empty-3.d b/gas/testsuite/gas/cfi-sframe/common-empty-3.d index 39caad5a1d4..c4a0fd85dd2 100644 --- a/gas/testsuite/gas/cfi-sframe/common-empty-3.d +++ b/gas/testsuite/gas/cfi-sframe/common-empty-3.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL #? CFA fixed FP offset: \-?\d+ #? CFA fixed RA offset: \-?\d+ Num FDEs: 0 diff --git a/gas/testsuite/gas/scfi/x86_64/scfi-cfi-sections-1.d b/gas/testsuite/gas/scfi/x86_64/scfi-cfi-sections-1.d index 7c247e33a6e..b81ee062733 100644 --- a/gas/testsuite/gas/scfi/x86_64/scfi-cfi-sections-1.d +++ b/gas/testsuite/gas/scfi/x86_64/scfi-cfi-sections-1.d @@ -7,7 +7,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 Num FDEs: 1 Num FREs: 5 diff --git a/gas/testsuite/gas/scfi/x86_64/scfi-dyn-stack-1.d b/gas/testsuite/gas/scfi/x86_64/scfi-dyn-stack-1.d index c6a9b53f4e0..44796668f10 100644 --- a/gas/testsuite/gas/scfi/x86_64/scfi-dyn-stack-1.d +++ b/gas/testsuite/gas/scfi/x86_64/scfi-dyn-stack-1.d @@ -8,7 +8,7 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: NONE + Flags: SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 Num FDEs: 1 Num FREs: 4 diff --git a/include/sframe-api.h b/include/sframe-api.h index 0dfdd289b82..1e21dfab1a0 100644 --- a/include/sframe-api.h +++ b/include/sframe-api.h @@ -44,6 +44,11 @@ typedef struct sframe_encoder_ctx sframe_encoder_ctx; (SFRAME_F_FDE_SORTED | SFRAME_F_FRAME_POINTER \ | SFRAME_F_FDE_FUNC_START_ADDR_PCREL) +/* Set of flags that are required to be harmonious between all decoder and + encoder objects participating in a link. */ +#define SFRAME_F_LD_MUSTHAVE_FLAGS \ + (SFRAME_F_FDE_FUNC_START_ADDR_PCREL) + /* User interfacing SFrame Row Entry. An abstraction provided by libsframe so the consumer is decoupled from the binary format representation of the same. diff --git a/ld/testsuite/ld-aarch64/sframe-simple-1.d b/ld/testsuite/ld-aarch64/sframe-simple-1.d index 6ab8c2ab46a..27c8a330c59 100644 --- a/ld/testsuite/ld-aarch64/sframe-simple-1.d +++ b/ld/testsuite/ld-aarch64/sframe-simple-1.d @@ -11,7 +11,8 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: SFRAME_F_FDE_SORTED + Flags: SFRAME_F_FDE_SORTED, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL Num FDEs: 2 Num FREs: 2 diff --git a/ld/testsuite/ld-x86-64/sframe-ibt-plt-1.d b/ld/testsuite/ld-x86-64/sframe-ibt-plt-1.d index f8682421245..8a767aca685 100644 --- a/ld/testsuite/ld-x86-64/sframe-ibt-plt-1.d +++ b/ld/testsuite/ld-x86-64/sframe-ibt-plt-1.d @@ -10,7 +10,8 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: SFRAME_F_FDE_SORTED + Flags: SFRAME_F_FDE_SORTED, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 #... diff --git a/ld/testsuite/ld-x86-64/sframe-plt-1.d b/ld/testsuite/ld-x86-64/sframe-plt-1.d index 2918ccba1eb..916bf96aacb 100644 --- a/ld/testsuite/ld-x86-64/sframe-plt-1.d +++ b/ld/testsuite/ld-x86-64/sframe-plt-1.d @@ -11,7 +11,8 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: SFRAME_F_FDE_SORTED + Flags: SFRAME_F_FDE_SORTED, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 #... diff --git a/ld/testsuite/ld-x86-64/sframe-pltgot-1.d b/ld/testsuite/ld-x86-64/sframe-pltgot-1.d index 6c47770c2b5..3193107cdf4 100644 --- a/ld/testsuite/ld-x86-64/sframe-pltgot-1.d +++ b/ld/testsuite/ld-x86-64/sframe-pltgot-1.d @@ -10,7 +10,8 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: SFRAME_F_FDE_SORTED + Flags: SFRAME_F_FDE_SORTED, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 #... diff --git a/ld/testsuite/ld-x86-64/sframe-pltgot-2.d b/ld/testsuite/ld-x86-64/sframe-pltgot-2.d index 141be292230..4a97389f2eb 100644 --- a/ld/testsuite/ld-x86-64/sframe-pltgot-2.d +++ b/ld/testsuite/ld-x86-64/sframe-pltgot-2.d @@ -10,7 +10,8 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: SFRAME_F_FDE_SORTED + Flags: SFRAME_F_FDE_SORTED, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 #... diff --git a/ld/testsuite/ld-x86-64/sframe-simple-1.d b/ld/testsuite/ld-x86-64/sframe-simple-1.d index 3effdbf0529..f2a1ef7fbd8 100644 --- a/ld/testsuite/ld-x86-64/sframe-simple-1.d +++ b/ld/testsuite/ld-x86-64/sframe-simple-1.d @@ -11,7 +11,8 @@ Contents of the SFrame section .sframe: Header : Version: SFRAME_VERSION_2 - Flags: SFRAME_F_FDE_SORTED + Flags: SFRAME_F_FDE_SORTED, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL CFA fixed RA offset: \-8 #... diff --git a/libsframe/sframe-dump.c b/libsframe/sframe-dump.c index bc0d5e46b03..9048931a8ba 100644 --- a/libsframe/sframe-dump.c +++ b/libsframe/sframe-dump.c @@ -133,8 +133,15 @@ dump_sframe_func_with_fres (sframe_decoder_ctx *sfd_ctx, /* Get the SFrame function descriptor. */ sframe_decoder_get_funcdesc (sfd_ctx, funcidx, &num_fres, &func_size, &func_start_address, &func_info); - /* Calculate the virtual memory address for function start pc. */ +/* Calculate the virtual memory address for function start pc. Some older + SFrame V2 sections in ET_DYN or ET_EXEC may still have the + SFRAME_F_FDE_FUNC_START_ADDR_PCREL flag unset, and hence may be using the + old encoding. Continue to support dumping the sections at least. */ func_start_pc_vma = func_start_address + sec_addr; + if (sframe_decoder_get_flags (sfd_ctx) & SFRAME_F_FDE_FUNC_START_ADDR_PCREL) + func_start_pc_vma += sframe_decoder_get_offsetof_fde_start_addr (sfd_ctx, + funcidx, + NULL); /* Mark FDEs with [m] where the FRE start address is interpreted as a mask. */ diff --git a/libsframe/sframe.c b/libsframe/sframe.c index 8be7bec8917..946b3cf16b3 100644 --- a/libsframe/sframe.c +++ b/libsframe/sframe.c @@ -363,25 +363,47 @@ sframe_decoder_get_funcdesc_at_index (sframe_decoder_ctx *ctx, return fdep; } +/* Get the offset of the start PC of the SFrame FDE at FUNC_IDX from the start + of the SFrame section. This section-relative offset is used within + libsframe for sorting the SFrame FDEs, and also information lookup routines + like sframe_find_fre. + + If FUNC_IDX is not a valid index in the given decoder object, returns 0. */ + +static int32_t +sframe_decoder_get_secrel_func_start_addr (sframe_decoder_ctx *dctx, + uint32_t func_idx) +{ + int err = 0; + int32_t offsetof_fde_in_sec + = sframe_decoder_get_offsetof_fde_start_addr (dctx, func_idx, &err); + /* If func_idx is not a valid index, return 0. */ + if (err) + return 0; + + int32_t func_start_addr = dctx->sfd_funcdesc[func_idx].sfde_func_start_address; + + return func_start_addr + offsetof_fde_in_sec; +} + /* Check whether for the given FDEP, the SFrame Frame Row Entry identified via the START_IP_OFFSET and the END_IP_OFFSET, provides the stack trace information for the PC. */ static bool -sframe_fre_check_range_p (sframe_func_desc_entry *fdep, +sframe_fre_check_range_p (sframe_decoder_ctx *dctx, uint32_t func_idx, uint32_t start_ip_offset, uint32_t end_ip_offset, int32_t pc) { + sframe_func_desc_entry *fdep; int32_t func_start_addr; uint8_t rep_block_size; uint32_t fde_type; uint32_t pc_offset; bool mask_p; - if (!fdep) - return false; - - func_start_addr = fdep->sfde_func_start_address; + fdep = &dctx->sfd_funcdesc[func_idx]; + func_start_addr = sframe_decoder_get_secrel_func_start_addr (dctx, func_idx); fde_type = sframe_get_fde_type (fdep); mask_p = (fde_type == SFRAME_FDE_TYPE_PCMASK); rep_block_size = fdep->sfde_func_rep_size; @@ -1055,7 +1077,7 @@ sframe_get_funcdesc_with_addr (sframe_decoder_ctx *ctx __attribute__ ((unused)), static sframe_func_desc_entry * sframe_get_funcdesc_with_addr_internal (sframe_decoder_ctx *ctx, int32_t addr, - int *errp) + int *errp, uint32_t *func_idx) { sframe_header *dhp; sframe_func_desc_entry *fdp; @@ -1083,12 +1105,15 @@ sframe_get_funcdesc_with_addr_internal (sframe_decoder_ctx *ctx, int32_t addr, /* Given sfde_func_start_address <= addr, addr - sfde_func_start_address must be positive. */ - if (fdp[mid].sfde_func_start_address <= addr - && ((uint32_t)(addr - fdp[mid].sfde_func_start_address) + if (sframe_decoder_get_secrel_func_start_addr (ctx, mid) <= addr + && ((uint32_t)(addr - sframe_decoder_get_secrel_func_start_addr (ctx, mid)) < fdp[mid].sfde_func_size)) - return fdp + mid; + { + *func_idx = mid; + return fdp + mid; + } - if (fdp[mid].sfde_func_start_address < addr) + if (sframe_decoder_get_secrel_func_start_addr (ctx, mid) < addr) low = mid + 1; else high = mid - 1; @@ -1132,6 +1157,7 @@ sframe_find_fre (sframe_decoder_ctx *ctx, int32_t pc, { sframe_frame_row_entry cur_fre; sframe_func_desc_entry *fdep; + uint32_t func_idx; uint32_t fre_type, i; int32_t func_start_addr; uint32_t start_ip_offset, end_ip_offset; @@ -1143,14 +1169,14 @@ sframe_find_fre (sframe_decoder_ctx *ctx, int32_t pc, return sframe_set_errno (&err, SFRAME_ERR_INVAL); /* Find the FDE which contains the PC, then scan its fre entries. */ - fdep = sframe_get_funcdesc_with_addr_internal (ctx, pc, &err); + fdep = sframe_get_funcdesc_with_addr_internal (ctx, pc, &err, &func_idx); if (fdep == NULL || ctx->sfd_fres == NULL) return sframe_set_errno (&err, SFRAME_ERR_DCTX_INVAL); fre_type = sframe_get_fre_type (fdep); fres = ctx->sfd_fres + fdep->sfde_func_start_fre_off; - func_start_addr = fdep->sfde_func_start_address; + func_start_addr = sframe_decoder_get_secrel_func_start_addr (ctx, func_idx); for (i = 0; i < fdep->sfde_func_num_fres; i++) { @@ -1166,7 +1192,7 @@ sframe_find_fre (sframe_decoder_ctx *ctx, int32_t pc, if (start_ip_offset > (uint32_t)(pc - func_start_addr)) return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL); - if (sframe_fre_check_range_p (fdep, start_ip_offset, end_ip_offset, pc)) + if (sframe_fre_check_range_p (ctx, func_idx, start_ip_offset, end_ip_offset, pc)) { sframe_frame_row_entry_copy (frep, &cur_fre); return 0; @@ -1362,6 +1388,12 @@ sframe_encode (uint8_t ver, uint8_t flags, uint8_t abi_arch, hp->sfh_preamble.sfp_magic = SFRAME_MAGIC; hp->sfh_preamble.sfp_flags = flags; + /* Implementation in the SFrame encoder APIs, e.g., + sframe_encoder_write_sframe assume flag SFRAME_F_FDE_FUNC_START_ADDR_PCREL + set. */ + if (!(flags & SFRAME_F_FDE_FUNC_START_ADDR_PCREL)) + return sframe_ret_set_errno (errp, SFRAME_ERR_ECTX_INVAL); + hp->sfh_abi_arch = abi_arch; hp->sfh_cfa_fixed_fp_offset = fixed_fp_offset; hp->sfh_cfa_fixed_ra_offset = fixed_ra_offset; @@ -1691,15 +1723,23 @@ sframe_encoder_add_funcdesc_v2 (sframe_encoder_ctx *encoder, static int sframe_sort_funcdesc (sframe_encoder_ctx *encoder) { - sframe_header *ehp; + sframe_header *ehp = sframe_encoder_get_header (encoder); - ehp = sframe_encoder_get_header (encoder); /* Sort and write out the FDE table. */ sf_fde_tbl *fd_info = encoder->sfe_funcdesc; if (fd_info) { + for (unsigned int i = 0; i < fd_info->count; i++) + fd_info->entry[i].sfde_func_start_address + += sframe_encoder_get_offsetof_fde_start_addr (encoder, i, NULL); + qsort (fd_info->entry, fd_info->count, sizeof (sframe_func_desc_entry), fde_func); + + for (unsigned int i = 0; i < fd_info->count; i++) + fd_info->entry[i].sfde_func_start_address + -= sframe_encoder_get_offsetof_fde_start_addr (encoder, i, NULL); + /* Update preamble's flags. */ ehp->sfh_preamble.sfp_flags |= SFRAME_F_FDE_SORTED; } diff --git a/libsframe/testsuite/libsframe.decode/DATA2 b/libsframe/testsuite/libsframe.decode/DATA2 index 472f736f872..90649e27169 100644 Binary files a/libsframe/testsuite/libsframe.decode/DATA2 and b/libsframe/testsuite/libsframe.decode/DATA2 differ diff --git a/libsframe/testsuite/libsframe.encode/encode-1.c b/libsframe/testsuite/libsframe.encode/encode-1.c index 3c2df7619b8..ffc332d2d25 100644 --- a/libsframe/testsuite/libsframe.encode/encode-1.c +++ b/libsframe/testsuite/libsframe.encode/encode-1.c @@ -41,7 +41,7 @@ add_fde1 (sframe_encoder_ctx *encode, int idx) unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); - err = sframe_encoder_add_funcdesc (encode, 0xfffff03e, 0x1b, finfo, 4); + err = sframe_encoder_add_funcdesc (encode, 0xfffff022, 0x1b, finfo, 4); if (err == -1) return err; @@ -66,7 +66,7 @@ add_fde2 (sframe_encoder_ctx *encode, int idx) unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); - err = sframe_encoder_add_funcdesc (encode, 0xfffff059, 0x10, finfo, 4); + err = sframe_encoder_add_funcdesc (encode, 0xfffff029, 0x10, finfo, 4); if (err == -1) return err; @@ -145,7 +145,8 @@ int main (void) } \ while (0) - encode = sframe_encode (SFRAME_VERSION, 0, + encode = sframe_encode (SFRAME_VERSION, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL, SFRAME_ABI_AMD64_ENDIAN_LITTLE, SFRAME_CFA_FIXED_FP_INVALID, -8, /* Fixed RA offset for AMD64. */ diff --git a/libsframe/testsuite/libsframe.find/findfre-1.c b/libsframe/testsuite/libsframe.find/findfre-1.c index 94fe2e9493f..b2ab045ae7e 100644 --- a/libsframe/testsuite/libsframe.find/findfre-1.c +++ b/libsframe/testsuite/libsframe.find/findfre-1.c @@ -43,7 +43,10 @@ add_fde1 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr, fre_start_addr of the last FRE above (0x38). */ *func_size = 0x40; - int32_t func1_start_addr = start_pc_vaddr - sframe_vaddr; + uint32_t offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL); + int32_t func1_start_addr = (start_pc_vaddr + - (sframe_vaddr + offsetof_fde_in_sec)); unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); int err = sframe_encoder_add_funcdesc (encode, func1_start_addr, *func_size, @@ -74,7 +77,10 @@ add_fde2 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr, fre_start_addr of the last FRE above (0x20). */ *func_size = 0x60; - int32_t func2_start_addr = start_pc_vaddr - sframe_vaddr; + uint32_t offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL); + int32_t func2_start_addr = (start_pc_vaddr + - (sframe_vaddr + offsetof_fde_in_sec)); unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); int err = sframe_encoder_add_funcdesc (encode, func2_start_addr, *func_size, @@ -115,7 +121,8 @@ void test_text_findfre (uint32_t text_vaddr, uint32_t sframe_vaddr) } \ while (0) - encode = sframe_encode (SFRAME_VERSION, 0, + encode = sframe_encode (SFRAME_VERSION, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL, SFRAME_ABI_AMD64_ENDIAN_LITTLE, SFRAME_CFA_FIXED_FP_INVALID, -8, /* Fixed RA offset for AMD64. */ diff --git a/libsframe/testsuite/libsframe.find/findfunc-1.c b/libsframe/testsuite/libsframe.find/findfunc-1.c index 3cdcdb6631e..f9b92c18333 100644 --- a/libsframe/testsuite/libsframe.find/findfunc-1.c +++ b/libsframe/testsuite/libsframe.find/findfunc-1.c @@ -50,7 +50,10 @@ add_fde1 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr, fre_start_addr of the last FRE above (0x38). */ *func_size = 0x40; - int32_t func1_start_addr = start_pc_vaddr - sframe_vaddr; + uint32_t offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL); + int32_t func1_start_addr = (start_pc_vaddr + - (sframe_vaddr + offsetof_fde_in_sec)); unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); int err = sframe_encoder_add_funcdesc (encode, func1_start_addr, *func_size, @@ -81,7 +84,10 @@ add_fde2 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr, fre_start_addr of the last FRE above (0x20). */ *func_size = 0x60; - int32_t func2_start_addr = start_pc_vaddr - sframe_vaddr; + uint32_t offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL); + int32_t func2_start_addr = (start_pc_vaddr + - (sframe_vaddr + offsetof_fde_in_sec)); unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); int err = sframe_encoder_add_funcdesc (encode, func2_start_addr, *func_size, @@ -112,7 +118,10 @@ add_fde3 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr, fre_start_addr of the last FRE above (0x38). */ *func_size = 0x40; - int32_t func3_start_addr = start_pc_vaddr - sframe_vaddr; + uint32_t offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL); + int32_t func3_start_addr = (start_pc_vaddr + - (sframe_vaddr + offsetof_fde_in_sec)); unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCINC); int err = sframe_encoder_add_funcdesc (encode, func3_start_addr, *func_size, @@ -155,7 +164,8 @@ void test_text_findfre (uint32_t text_vaddr, uint32_t sframe_vaddr) } \ while (0) - encode = sframe_encode (SFRAME_VERSION, 0, + encode = sframe_encode (SFRAME_VERSION, + SFRAME_F_FDE_FUNC_START_ADDR_PCREL, SFRAME_ABI_AMD64_ENDIAN_LITTLE, SFRAME_CFA_FIXED_FP_INVALID, -8, /* Fixed RA offset for AMD64. */ diff --git a/libsframe/testsuite/libsframe.find/plt-findfre-1.c b/libsframe/testsuite/libsframe.find/plt-findfre-1.c index 89ca4661922..710cc6d8352 100644 --- a/libsframe/testsuite/libsframe.find/plt-findfre-1.c +++ b/libsframe/testsuite/libsframe.find/plt-findfre-1.c @@ -42,7 +42,10 @@ add_plt_fde1 (sframe_encoder_ctx *ectx, uint32_t plt_vaddr, unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, SFRAME_FDE_TYPE_PCMASK); - int32_t func_start_addr = plt_vaddr - sframe_vaddr; + uint32_t offsetof_fde_in_sec + = sframe_encoder_get_offsetof_fde_start_addr (ectx, idx, NULL); + int32_t func_start_addr = (plt_vaddr + - (sframe_vaddr + offsetof_fde_in_sec)); /* 5 pltN entries of 16 bytes each. */ int err = sframe_encoder_add_funcdesc_v2 (ectx, func_start_addr, @@ -81,7 +84,8 @@ void test_plt_findfre (uint32_t plt_vaddr, uint32_t sframe_vaddr) } \ while (0) - ectx = sframe_encode (SFRAME_VERSION, 0, SFRAME_ABI_AMD64_ENDIAN_LITTLE, + ectx = sframe_encode (SFRAME_VERSION, SFRAME_F_FDE_FUNC_START_ADDR_PCREL, + SFRAME_ABI_AMD64_ENDIAN_LITTLE, SFRAME_CFA_FIXED_FP_INVALID, -8, /* Fixed RA offset for AMD64. */ &err);