1 /* ldbuildid.c - Build Id support routines
2 Copyright (C) 2013-2025 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "safe-ctype.h"
27 #define XXH_INLINE_ALL
30 #include "ldbuildid.h"
36 #define streq(a,b) strcmp ((a), (b)) == 0
39 validate_build_id_style (const char *style
)
41 if ((streq (style
, "md5")) || (streq (style
, "sha1"))
43 || (streq (style
, "xx"))
45 || (streq (style
, "uuid")) || (startswith (style
, "0x")))
52 compute_build_id_size (const char *style
)
54 if (streq (style
, "md5") || streq (style
, "uuid"))
58 if (streq (style
, "xx"))
62 if (streq (style
, "sha1"))
65 if (startswith (style
, "0x"))
67 bfd_size_type size
= 0;
68 /* ID is in string form (hex). Count the bytes. */
69 const char *id
= style
+ 2;
73 if (ISXDIGIT (id
[0]) && ISXDIGIT (id
[1]))
78 else if (*id
== '-' || *id
== ':')
85 } while (*id
!= '\0');
93 read_hex (const char xdigit
)
99 return xdigit
- 'A' + 0xa;
101 if (ISLOWER (xdigit
))
102 return xdigit
- 'a' + 0xa;
111 xx_process_bytes(const void* buffer
, size_t size
, void* state
)
113 XXH3_128bits_update ((XXH3_state_t
*) state
, buffer
, size
);
119 generate_build_id (bfd
*abfd
,
121 checksum_fn checksum_contents
,
122 unsigned char *id_bits
,
123 int size ATTRIBUTE_UNUSED
)
126 if (streq (style
, "xx"))
128 XXH3_state_t
* state
= XXH3_createState ();
133 XXH3_128bits_reset (state
);
134 if (!(*checksum_contents
) (abfd
, &xx_process_bytes
, state
))
136 XXH3_freeState (state
);
139 XXH128_hash_t result
= XXH3_128bits_digest (state
);
140 XXH3_freeState (state
);
141 /* Use canonical-endianness output. */
142 XXH128_canonical_t result_canon
;
143 XXH128_canonicalFromHash (&result_canon
, result
);
144 memcpy (id_bits
, &result_canon
,
145 (size_t) size
< sizeof (result
) ? (size_t) size
: sizeof (result
));
149 if (streq (style
, "md5"))
154 if (!(*checksum_contents
) (abfd
, (sum_fn
) &md5_process_bytes
, &ctx
))
156 md5_finish_ctx (&ctx
, id_bits
);
158 else if (streq (style
, "sha1"))
162 sha1_init_ctx (&ctx
);
163 if (!(*checksum_contents
) (abfd
, (sum_fn
) sha1_choose_process_bytes (),
166 sha1_finish_ctx (&ctx
, id_bits
);
168 else if (streq (style
, "uuid"))
172 int fd
= open ("/dev/urandom", O_RDONLY
);
176 n
= read (fd
, id_bits
, size
);
180 #else /* __MINGW32__ */
181 typedef RPC_STATUS (RPC_ENTRY
* UuidCreateFn
) (UUID
*);
183 UuidCreateFn uuid_create
= 0;
184 HMODULE rpc_library
= LoadLibrary ("rpcrt4.dll");
188 uuid_create
= (UuidCreateFn
) (void (WINAPI
*)(void)) GetProcAddress (rpc_library
, "UuidCreate");
191 FreeLibrary (rpc_library
);
195 if (uuid_create (&uuid
) != RPC_S_OK
)
197 FreeLibrary (rpc_library
);
200 FreeLibrary (rpc_library
);
201 memcpy (id_bits
, &uuid
,
202 (size_t) size
< sizeof (UUID
) ? (size_t) size
: sizeof (UUID
));
203 #endif /* __MINGW32__ */
205 else if (startswith (style
, "0x"))
207 /* ID is in string form (hex). Convert to bits. */
208 const char *id
= style
+ 2;
213 if (ISXDIGIT (id
[0]) && ISXDIGIT (id
[1]))
215 id_bits
[n
] = read_hex (*id
++) << 4;
216 id_bits
[n
++] |= read_hex (*id
++);
218 else if (*id
== '-' || *id
== ':')
221 abort (); /* Should have been validated earlier. */
226 abort (); /* Should have been validated earlier. */