Skip to content

Commit 1e60adc

Browse files
kito-chenglgaver2
authored andcommitted
RISC-V: Implment the merge logic for GNU_PROPERTY_RISCV_FEATURE_1_AND
GNU_PROPERTY_RISCV_FEATURE_1_AND will perform a bitwise AND operation on the properties of the input files.
1 parent a4ed0f1 commit 1e60adc

15 files changed

+383
-0
lines changed

bfd/elfnn-riscv.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ struct _bfd_riscv_elf_obj_tdata
178178
/* tls_type for each local got entry. */
179179
char *local_got_tls_type;
180180

181+
/* All GNU_PROPERTY_RISCV_FEATURE_1_AND properties. */
182+
uint32_t gnu_and_prop;
181183
/* PLT type. */
182184
riscv_plt_type plt_type;
183185
};
@@ -5841,6 +5843,36 @@ riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
58415843
h->other |= STO_RISCV_VARIANT_CC;
58425844
}
58435845

5846+
/* Implement elf_backend_setup_gnu_properties for RISC-V. It serves as a
5847+
wrapper function for _bfd_riscv_elf_link_setup_gnu_properties to account
5848+
for the effect of GNU properties of the output_bfd. */
5849+
5850+
static bfd *
5851+
elfNN_riscv_link_setup_gnu_properties (struct bfd_link_info *info)
5852+
{
5853+
uint32_t and_prop = _bfd_riscv_elf_tdata (info->output_bfd)->gnu_and_prop;
5854+
5855+
bfd *pbfd = _bfd_riscv_elf_link_setup_gnu_properties (info, &and_prop);
5856+
5857+
_bfd_riscv_elf_tdata (info->output_bfd)->gnu_and_prop = and_prop;
5858+
return pbfd;
5859+
}
5860+
5861+
/* Implement elf_backend_merge_gnu_properties for RISC-V. It serves as a
5862+
wrapper function for _bfd_riscv_elf_merge_gnu_properties to account
5863+
for the effect of GNU properties of the output_bfd. */
5864+
5865+
static bool
5866+
elfNN_riscv_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd,
5867+
bfd *bbfd ATTRIBUTE_UNUSED,
5868+
elf_property *aprop, elf_property *bprop)
5869+
{
5870+
uint32_t and_prop = _bfd_riscv_elf_tdata (info->output_bfd)->gnu_and_prop;
5871+
5872+
return _bfd_riscv_elf_merge_gnu_properties (info, abfd, aprop, bprop,
5873+
and_prop);
5874+
}
5875+
58445876
#define TARGET_LITTLE_SYM riscv_elfNN_vec
58455877
#define TARGET_LITTLE_NAME "elfNN-littleriscv"
58465878
#define TARGET_BIG_SYM riscv_elfNN_be_vec
@@ -5880,6 +5912,9 @@ riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
58805912

58815913
#define elf_backend_init_index_section _bfd_elf_init_1_index_section
58825914

5915+
#define elf_backend_setup_gnu_properties elfNN_riscv_link_setup_gnu_properties
5916+
#define elf_backend_merge_gnu_properties elfNN_riscv_merge_gnu_properties
5917+
58835918
#define elf_backend_can_gc_sections 1
58845919
#define elf_backend_can_refcount 1
58855920
#define elf_backend_want_got_plt 1

bfd/elfxx-riscv.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,3 +3463,174 @@ riscv_print_extensions (void)
34633463
}
34643464
printf ("\n");
34653465
}
3466+
3467+
/* Find the first input bfd with GNU property and merge it with GPROP. If no
3468+
such input is found, add it to a new section at the last input. Update
3469+
GPROP accordingly. */
3470+
3471+
bfd *
3472+
_bfd_riscv_elf_link_setup_gnu_properties (struct bfd_link_info *info,
3473+
uint32_t *and_prop_p)
3474+
{
3475+
asection *sec;
3476+
bfd *pbfd;
3477+
bfd *ebfd = NULL;
3478+
elf_property *prop;
3479+
3480+
uint32_t and_prop = *and_prop_p;
3481+
3482+
/* Find a normal input file with GNU property note. */
3483+
for (pbfd = info->input_bfds; pbfd != NULL; pbfd = pbfd->link.next)
3484+
if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
3485+
&& bfd_count_sections (pbfd) != 0)
3486+
{
3487+
ebfd = pbfd;
3488+
3489+
if (elf_properties (pbfd) != NULL)
3490+
break;
3491+
}
3492+
3493+
/* If ebfd != NULL it is either an input with property note or the last
3494+
input. Either way if we have and_prop, we should add it (by
3495+
creating a section if needed). */
3496+
if (ebfd != NULL && (and_prop))
3497+
{
3498+
prop = _bfd_elf_get_property (ebfd, GNU_PROPERTY_RISCV_FEATURE_1_AND, 4);
3499+
3500+
prop->u.number |= and_prop;
3501+
prop->pr_kind = property_number;
3502+
3503+
/* pbfd being NULL implies ebfd is the last input. Create the GNU
3504+
property note section. */
3505+
if (pbfd == NULL)
3506+
{
3507+
sec
3508+
= bfd_make_section_with_flags (ebfd, NOTE_GNU_PROPERTY_SECTION_NAME,
3509+
(SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
3510+
| SEC_READONLY | SEC_HAS_CONTENTS
3511+
| SEC_DATA));
3512+
if (sec == NULL)
3513+
info->callbacks->einfo (
3514+
_ ("%F%P: failed to create GNU property section\n"));
3515+
3516+
elf_section_type (sec) = SHT_NOTE;
3517+
}
3518+
}
3519+
3520+
pbfd = _bfd_elf_link_setup_gnu_properties (info);
3521+
3522+
if (bfd_link_relocatable (info))
3523+
return pbfd;
3524+
3525+
/* If pbfd has any GNU_PROPERTY_RISCV_FEATURE_1_AND properties, update
3526+
and_prop accordingly. */
3527+
if (pbfd != NULL)
3528+
{
3529+
elf_property_list *p;
3530+
elf_property_list *plist = elf_properties (pbfd);
3531+
3532+
if ((p = _bfd_elf_find_property (plist, GNU_PROPERTY_RISCV_FEATURE_1_AND,
3533+
NULL))
3534+
!= NULL)
3535+
and_prop = p->property.u.number
3536+
& (GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED
3537+
| GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS);
3538+
}
3539+
3540+
*and_prop_p = and_prop;
3541+
return pbfd;
3542+
}
3543+
3544+
/* Define elf_backend_parse_gnu_properties for RISC-V. */
3545+
3546+
enum elf_property_kind
3547+
_bfd_riscv_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3548+
bfd_byte *ptr, unsigned int datasz)
3549+
{
3550+
elf_property *prop;
3551+
3552+
switch (type)
3553+
{
3554+
case GNU_PROPERTY_RISCV_FEATURE_1_AND:
3555+
if (datasz != 4)
3556+
{
3557+
_bfd_error_handler (_ (
3558+
"error: %pB: <corrupt RISC-V used size: 0x%x>"),
3559+
abfd, datasz);
3560+
return property_corrupt;
3561+
}
3562+
prop = _bfd_elf_get_property (abfd, type, datasz);
3563+
/* Combine properties of the same type. */
3564+
prop->u.number |= bfd_h_get_32 (abfd, ptr);
3565+
prop->pr_kind = property_number;
3566+
break;
3567+
3568+
default:
3569+
return property_ignored;
3570+
}
3571+
3572+
return property_number;
3573+
}
3574+
3575+
/* Merge RISC-V GNU property BPROP with APROP also accounting for PROP.
3576+
If APROP isn't NULL, merge it with BPROP and/or PROP. Vice-versa if BROP
3577+
isn't NULL. Return TRUE if there is any update to APROP or if BPROP should
3578+
be merge with ABFD. */
3579+
3580+
bool
3581+
_bfd_riscv_elf_merge_gnu_properties
3582+
(struct bfd_link_info *info ATTRIBUTE_UNUSED, bfd *abfd ATTRIBUTE_UNUSED,
3583+
elf_property *aprop, elf_property *bprop, uint32_t and_prop)
3584+
{
3585+
unsigned int orig_number;
3586+
bool updated = false;
3587+
unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3588+
3589+
switch (pr_type)
3590+
{
3591+
case GNU_PROPERTY_RISCV_FEATURE_1_AND: {
3592+
if (aprop != NULL && bprop != NULL)
3593+
{
3594+
orig_number = aprop->u.number;
3595+
aprop->u.number = (orig_number & bprop->u.number) | and_prop;
3596+
updated = orig_number != aprop->u.number;
3597+
/* Remove the property if all feature bits are cleared. */
3598+
if (aprop->u.number == 0)
3599+
aprop->pr_kind = property_remove;
3600+
break;
3601+
}
3602+
/* If either is NULL, the AND would be 0 so, if there is
3603+
any PROP, asign it to the input that is not NULL. */
3604+
if (and_prop)
3605+
{
3606+
if (aprop != NULL)
3607+
{
3608+
orig_number = aprop->u.number;
3609+
aprop->u.number = and_prop;
3610+
updated = orig_number != aprop->u.number;
3611+
}
3612+
else if (bprop != NULL)
3613+
{
3614+
bprop->u.number = and_prop;
3615+
updated = true;
3616+
}
3617+
/* Shouldn't happen because we checked one of APROP or BPROP !=
3618+
* NULL. */
3619+
else
3620+
abort ();
3621+
}
3622+
/* No PROP and BPROP is NULL, so remove APROP. */
3623+
else if (!and_prop && bprop == NULL && aprop != NULL)
3624+
{
3625+
aprop->pr_kind = property_remove;
3626+
updated = true;
3627+
}
3628+
}
3629+
break;
3630+
3631+
default:
3632+
abort ();
3633+
}
3634+
3635+
return updated;
3636+
}

bfd/elfxx-riscv.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,16 @@ extern void
137137
bfd_elf64_riscv_set_data_segment_info (struct bfd_link_info *, int *);
138138
extern void
139139
bfd_elf128_riscv_set_data_segment_info (struct bfd_link_info *, int *);
140+
141+
extern bfd *
142+
_bfd_riscv_elf_link_setup_gnu_properties (struct bfd_link_info *, uint32_t *);
143+
144+
extern enum elf_property_kind
145+
_bfd_riscv_elf_parse_gnu_properties (bfd *, unsigned int, bfd_byte *,
146+
unsigned int);
147+
148+
extern bool
149+
_bfd_riscv_elf_merge_gnu_properties (struct bfd_link_info *, bfd *,
150+
elf_property *, elf_property *, uint32_t);
151+
152+
#define elf_backend_parse_gnu_properties _bfd_riscv_elf_parse_gnu_properties

ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ if [istarget "riscv*-*-*"] {
227227
run_dump_test "data-reloc-rv64-addr32-pic"
228228
run_dump_test "data-reloc-rv64-undef32-pic"
229229

230+
run_dump_test "property-zicfilp-unlabeled"
231+
run_dump_test "property-zicfiss"
232+
run_dump_test "property-combine-and-1"
233+
run_dump_test "property-combine-and-2"
234+
run_dump_test "property-combine-and-3"
235+
230236
# IFUNC testcases.
231237
# Check IFUNC by single type relocs.
232238
run_dump_test_ifunc "ifunc-reloc-call-01" rv32 exe
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#name: RISC-V GNU Property (multiple inputs, combine section) - 1
2+
#source: property1.s
3+
#source: property2.s
4+
#as: -march=rv64g
5+
#ld: -shared
6+
#readelf: -n
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#name: RISC-V GNU Property (multiple inputs, combine section) - 2
2+
#source: property1.s
3+
#source: property3.s
4+
#as: -march=rv64g
5+
#ld: -shared
6+
#readelf: -n
7+
8+
Displaying notes found in: .note.gnu.property
9+
[ ]+Owner[ ]+Data size[ ]+Description
10+
[ ]+GNU[ ]+0x00000010[ ]+NT_GNU_PROPERTY_TYPE_0
11+
[ ]+Properties: RISC-V AND feature: CFI_LP_UNLABELED
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#name: RISC-V GNU Property (multiple inputs, combine section) - 3
2+
#source: property1.s
3+
#source: property4.s
4+
#as: -march=rv64g
5+
#ld: -shared
6+
#readelf: -n
7+
8+
Displaying notes found in: .note.gnu.property
9+
[ ]+Owner[ ]+Data size[ ]+Description
10+
[ ]+GNU[ ]+0x00000010[ ]+NT_GNU_PROPERTY_TYPE_0
11+
[ ]+Properties: RISC-V AND feature: CFI_SS
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#name: GNU Property (single input, CFI_LP_UNLABELED)
2+
#source: property-zicfilp-unlabeled.s
3+
#as: -march=rv64g
4+
#ld: -shared
5+
#readelf: -n
6+
7+
Displaying notes found in: .note.gnu.property
8+
[ ]+Owner[ ]+Data size[ ]+Description
9+
[ ]+GNU[ ]+0x00000010[ ]+NT_GNU_PROPERTY_TYPE_0
10+
[ ]+Properties: RISC-V AND feature: CFI_LP_UNLABELED
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.text
2+
.globl _start
3+
.type _start,@function
4+
_start:
5+
ret
6+
7+
.section ".note.gnu.property", "a"
8+
.p2align 3
9+
.long 1f - 0f /* name length */
10+
.long 5f - 2f /* data length */
11+
.long 5 /* note type */
12+
0: .asciz "GNU" /* vendor name */
13+
1:
14+
.p2align 3
15+
2: .long 0xc0000000 /* pr_type. */
16+
.long 4f - 3f /* pr_datasz. */
17+
3:
18+
.long 0x1 /* GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED. */
19+
4:
20+
.p2align 3
21+
5:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#name: GNU Property (single input, CFI_SS)
2+
#source: property-zicfiss.s
3+
#as: -march=rv64g
4+
#ld: -shared
5+
#readelf: -n
6+
7+
Displaying notes found in: .note.gnu.property
8+
[ ]+Owner[ ]+Data size[ ]+Description
9+
[ ]+GNU[ ]+0x00000010[ ]+NT_GNU_PROPERTY_TYPE_0
10+
[ ]+Properties: RISC-V AND feature: CFI_SS

0 commit comments

Comments
 (0)