From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2103) id B2A063858405; Thu, 28 Apr 2022 10:55:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2A063858405 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nick Alcock To: bfd-cvs@sourceware.org, gdb-cvs@sourceware.org Subject: [binutils-gdb] libctf: impose an ordering on conflicting types X-Act-Checkin: binutils-gdb X-Git-Author: Nick Alcock X-Git-Refname: refs/heads/master X-Git-Oldrev: 44c70fb01ff2ec9bced4676cdde0fa7e49328003 X-Git-Newrev: 95ade9a5f4becb3905a9261ead9b274347010636 Message-Id: <20220428105506.B2A063858405@sourceware.org> Date: Thu, 28 Apr 2022 10:55:06 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Apr 2022 10:55:06 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D95ade9a5f4be= cb3905a9261ead9b274347010636 commit 95ade9a5f4becb3905a9261ead9b274347010636 Author: Nick Alcock Date: Fri Apr 22 23:08:48 2022 +0100 libctf: impose an ordering on conflicting types =20 When two types conflict and they are not types which can have forwards (say, two arrays of different sizes with the same name in two different TUs) the CTF deduplicator uses a popularity contest to decide what to do: the type cited by the most other types ends up put into the shared dict, while the others are relegated to per-CU child dicts. =20 This works well as long as one type *is* most popular -- but what if there is a tie? If several types have the same popularity count, we end up picking the first we run across and promoting it, and unfortunately since we are working over a dynhash in essentially arbitrary order, this means we promote a random one. So multiple runs of ld with the same inputs can produce different outputs! All the outputs are valid, but this is still undesirable. =20 Adjust things to use the same strategy used to sort types on the output: when there is a tie, always put the type that appears in a CU that appeared earlier on the link line (and if there is somehow still a tie, which should be impossible, pick the type with the lowest type ID). =20 Add a testcase -- and since this emerged when trying out extern arrays, check that those work as well (this requires a newer GCC, but since all GCCs that can emit CTF at all are unreleased this is probably OK as well). =20 Fix up one testcase that has slight type ordering changes as a result of this change. =20 libctf/ChangeLog: =20 * ctf-dedup.c (ctf_dedup_detect_name_ambiguity): Use cd_output_first_gid to break ties. =20 ld/ChangeLog: =20 * testsuite/ld-ctf/array-conflicted-ordering.d: New test, using= ... * testsuite/ld-ctf/array-char-conflicting-1.c: ... this... * testsuite/ld-ctf/array-char-conflicting-2.c: ... and this. * testsuite/ld-ctf/array-extern.d: New test, using... * testsuite/ld-ctf/array-extern.c: ... this. * testsuite/ld-ctf/conflicting-typedefs.d: Adjust for ordering changes. Diff: --- ld/testsuite/ld-ctf/array-char-conflicting-1.c | 9 +++++++ ld/testsuite/ld-ctf/array-char-conflicting-2.c | 9 +++++++ ld/testsuite/ld-ctf/array-conflicted-ordering.d | 26 ++++++++++++++++++++ ld/testsuite/ld-ctf/array-extern.c | 1 + ld/testsuite/ld-ctf/array-extern.d | 32 +++++++++++++++++++++= ++++ ld/testsuite/ld-ctf/conflicting-typedefs.d | 2 +- libctf/ctf-dedup.c | 21 +++++++++++++++- 7 files changed, 98 insertions(+), 2 deletions(-) diff --git a/ld/testsuite/ld-ctf/array-char-conflicting-1.c b/ld/testsuite/= ld-ctf/array-char-conflicting-1.c new file mode 100644 index 00000000000..a6736a8a114 --- /dev/null +++ b/ld/testsuite/ld-ctf/array-char-conflicting-1.c @@ -0,0 +1,9 @@ +typedef char *array[10]; + +static array digits_names =3D {"zero", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine"}; + +void *foo (void) +{ + return digits_names; +} diff --git a/ld/testsuite/ld-ctf/array-char-conflicting-2.c b/ld/testsuite/= ld-ctf/array-char-conflicting-2.c new file mode 100644 index 00000000000..1cc46f0a31b --- /dev/null +++ b/ld/testsuite/ld-ctf/array-char-conflicting-2.c @@ -0,0 +1,9 @@ +typedef char *array[9]; + +static array digits_names =3D {"one", "two", "three", "four", + "five", "six", "seven", "eight", "nine"}; + +void *bar (void) +{ + return digits_names; +} diff --git a/ld/testsuite/ld-ctf/array-conflicted-ordering.d b/ld/testsuite= /ld-ctf/array-conflicted-ordering.d new file mode 100644 index 00000000000..a8bbc3dd65e --- /dev/null +++ b/ld/testsuite/ld-ctf/array-conflicted-ordering.d @@ -0,0 +1,26 @@ +#as: +#source: array-char-conflicting-1.c +#source: array-char-conflicting-2.c +#objdump: --ctf +#cc: -fPIC +#ld: -shared --ctf-variables --hash-style=3Dsysv +#name: Arrays (conflicted) + +.*: +file format .* + +Contents of CTF section .ctf: + + Header: + Magic number: 0xdff2 + Version: 4 \(CTF_VERSION_3\) +#... + Variables: + digits_names -> .* \(kind 4\) char \*\[10\] .* +#... + Header: +#... + Parent name: .ctf +#... + Variables: + digits_names -> .* \(kind 4\) char \*\[9\] .* +#... diff --git a/ld/testsuite/ld-ctf/array-extern.c b/ld/testsuite/ld-ctf/array= -extern.c new file mode 100644 index 00000000000..730ba5a7d81 --- /dev/null +++ b/ld/testsuite/ld-ctf/array-extern.c @@ -0,0 +1 @@ +extern char * digits_names[]; diff --git a/ld/testsuite/ld-ctf/array-extern.d b/ld/testsuite/ld-ctf/array= -extern.d new file mode 100644 index 00000000000..4c9ce784e6a --- /dev/null +++ b/ld/testsuite/ld-ctf/array-extern.d @@ -0,0 +1,32 @@ +#as: +#source: array-char.c +#source: array-extern.c +#objdump: --ctf +#ld: -shared --ctf-variables --hash-style=3Dsysv +#name: Arrays (extern) + +.*: +file format .* + +Contents of CTF section .ctf: + + Header: + Magic number: 0xdff2 + Version: 4 \(CTF_VERSION_3\) +#... + Data object section: .* \(0x[1-9a-f][0-9a-f]* bytes\) + Type section: .* \(0x44 bytes\) + String section: .* + + Labels: + + Data objects: + digits_names -> 0x[0-9a-f]*: \(kind 4\) char \*\[10\] .* + + Function objects: + + Variables: + + Types: +#... + 0x[0-9a-f]*: \(kind 4\) .*\[10\] \(size .* +#... diff --git a/ld/testsuite/ld-ctf/conflicting-typedefs.d b/ld/testsuite/ld-c= tf/conflicting-typedefs.d index 4ae8de41364..beb1f7776c6 100644 --- a/ld/testsuite/ld-ctf/conflicting-typedefs.d +++ b/ld/testsuite/ld-ctf/conflicting-typedefs.d @@ -15,8 +15,8 @@ Contents of CTF section .ctf: #... Types: 0x1: .*int .* - 0x[0-9]:.*int .* 0x[0-9]: \(kind 10\) word .* -> 0x[0-9]: \(kind 1\) .*int \(format 0x1= \) \(size 0x[0-9a-f]*\) \(aligned at 0x[0-9a-f]*\) + 0x[0-9]:.*int .* =20 Strings: #... diff --git a/libctf/ctf-dedup.c b/libctf/ctf-dedup.c index b2fb0a13441..cddf4376eae 100644 --- a/libctf/ctf-dedup.c +++ b/libctf/ctf-dedup.c @@ -1502,12 +1502,17 @@ ctf_dedup_detect_name_ambiguity (ctf_dict_t *fp, ct= f_dict_t **inputs) the most-popular type on insertion, and we want conflicting structs et al to have all forwards left intact, so the user is notified that this type is conflicting. TODO: improve this in future by - setting such forwards non-root-visible.) */ + setting such forwards non-root-visible.) + + If multiple distinct types are "most common", pick the one that + appears first on the link line, and within that, the one with the + lowest type ID. (See sort_output_mapping.) */ =20 const void *key; const void *count; const char *hval; long max_hcount =3D -1; + void *max_gid =3D NULL; const char *max_hval =3D NULL; =20 if (ctf_dynhash_elements (name_counts) <=3D 1) @@ -1517,10 +1522,24 @@ ctf_dedup_detect_name_ambiguity (ctf_dict_t *fp, ct= f_dict_t **inputs) while ((err =3D ctf_dynhash_cnext (name_counts, &j, &key, &count)) =3D= =3D 0) { hval =3D (const char *) key; + if ((long int) (uintptr_t) count > max_hcount) { max_hcount =3D (long int) (uintptr_t) count; max_hval =3D hval; + max_gid =3D ctf_dynhash_lookup (d->cd_output_first_gid, hval); + } + else if ((long int) (uintptr_t) count =3D=3D max_hcount) + { + void *gid =3D ctf_dynhash_lookup (d->cd_output_first_gid, hval); + + if (CTF_DEDUP_GID_TO_INPUT(gid) < CTF_DEDUP_GID_TO_INPUT(max_gid) + || (CTF_DEDUP_GID_TO_INPUT(gid) =3D=3D CTF_DEDUP_GID_TO_INPUT(max_= gid) + && CTF_DEDUP_GID_TO_TYPE(gid) < CTF_DEDUP_GID_TO_TYPE(max_gid))) + { + max_hval =3D hval; + max_gid =3D ctf_dynhash_lookup (d->cd_output_first_gid, hval); + } } } if (err !=3D ECTF_NEXT_END)