1 /* $NetBSD: libelf_ar.c,v 1.3 2016/02/20 02:43:42 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2006,2008,2010 Joseph Koshy 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #if HAVE_NBTOOL_CONFIG_H 30 # include "nbtool_config.h" 31 #endif 32 33 #include <sys/cdefs.h> 34 35 #include <assert.h> 36 #include <ctype.h> 37 #include <libelf.h> 38 #include <stdlib.h> 39 #include <string.h> 40 41 #include "_libelf.h" 42 #include "_libelf_ar.h" 43 44 __RCSID("$NetBSD: libelf_ar.c,v 1.3 2016/02/20 02:43:42 christos Exp $"); 45 ELFTC_VCSID("Id: libelf_ar.c 3174 2015-03-27 17:13:41Z emaste "); 46 47 #define LIBELF_NALLOC_SIZE 16 48 49 /* 50 * `ar' archive handling. 51 * 52 * `ar' archives start with signature `ARMAG'. Each archive member is 53 * preceded by a header containing meta-data for the member. This 54 * header is described in <ar.h> (struct ar_hdr). The header always 55 * starts on an even address. File data is padded with "\n" 56 * characters to keep this invariant. 57 * 58 * Special considerations for `ar' archives: 59 * 60 * There are two variants of the `ar' archive format: traditional BSD 61 * and SVR4. These differ in the way long file names are treated, and 62 * in the layout of the archive symbol table. 63 * 64 * The `ar' header only has space for a 16 character file name. 65 * 66 * In the SVR4 format, file names are terminated with a '/', so this 67 * effectively leaves 15 characters for the actual file name. Longer 68 * file names stored in a separate 'string table' and referenced 69 * indirectly from the name field. The string table itself appears as 70 * an archive member with name "// ". An `indirect' file name in an 71 * `ar' header matches the pattern "/[0-9]*". The digits form a 72 * decimal number that corresponds to a byte offset into the string 73 * table where the actual file name of the object starts. Strings in 74 * the string table are padded to start on even addresses. 75 * 76 * In the BSD format, file names can be upto 16 characters. File 77 * names shorter than 16 characters are padded to 16 characters using 78 * (ASCII) space characters. File names with embedded spaces and file 79 * names longer than 16 characters are stored immediately after the 80 * archive header and the name field set to a special indirect name 81 * matching the pattern "#1/[0-9]+". The digits form a decimal number 82 * that corresponds to the actual length of the file name following 83 * the archive header. The content of the archive member immediately 84 * follows the file name, and the size field of the archive member 85 * holds the sum of the sizes of the member and of the appended file 86 * name. 87 * 88 * Archives may also have a symbol table (see ranlib(1)), mapping 89 * program symbols to object files inside the archive. 90 * 91 * In the SVR4 format, a symbol table uses a file name of "/ " in its 92 * archive header. The symbol table is structured as: 93 * - a 4-byte count of entries stored as a binary value, MSB first 94 * - 'n' 4-byte offsets, stored as binary values, MSB first 95 * - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded. 96 * 97 * In the BSD format, the symbol table uses a file name of "__.SYMDEF". 98 * It is structured as two parts: 99 * - The first part is an array of "ranlib" structures preceded by 100 * the size of the array in bytes. Each "ranlib" structure 101 * describes one symbol. Each structure contains an offset into 102 * the string table for the symbol name, and a file offset into the 103 * archive for the member defining the symbol. 104 * - The second part is a string table containing NUL-terminated 105 * strings, preceded by the size of the string table in bytes. 106 * 107 * If the symbol table and string table are is present in an archive 108 * they must be the very first objects and in that order. 109 */ 110 111 112 /* 113 * Retrieve an archive header descriptor. 114 */ 115 116 Elf_Arhdr * 117 _libelf_ar_gethdr(Elf *e) 118 { 119 Elf *parent; 120 Elf_Arhdr *eh; 121 char *namelen; 122 size_t n, nlen; 123 struct ar_hdr *arh; 124 125 if ((parent = e->e_parent) == NULL) { 126 LIBELF_SET_ERROR(ARGUMENT, 0); 127 return (NULL); 128 } 129 130 assert((e->e_flags & LIBELF_F_AR_HEADER) == 0); 131 132 arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr; 133 134 assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG); 135 assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + 136 parent->e_rawsize - sizeof(struct ar_hdr)); 137 138 if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) { 139 LIBELF_SET_ERROR(RESOURCE, 0); 140 return (NULL); 141 } 142 143 e->e_hdr.e_arhdr = eh; 144 e->e_flags |= LIBELF_F_AR_HEADER; 145 146 eh->ar_name = eh->ar_rawname = NULL; 147 148 if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) == 149 NULL) 150 goto error; 151 152 if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, 153 &n) == 0) 154 goto error; 155 eh->ar_uid = (uid_t) n; 156 157 if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, 158 &n) == 0) 159 goto error; 160 eh->ar_gid = (gid_t) n; 161 162 if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, 163 &n) == 0) 164 goto error; 165 eh->ar_mode = (mode_t) n; 166 167 if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, 168 &n) == 0) 169 goto error; 170 171 /* 172 * Get the true size of the member if extended naming is being used. 173 */ 174 if (IS_EXTENDED_BSD_NAME(arh->ar_name)) { 175 namelen = arh->ar_name + 176 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE; 177 if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) - 178 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0) 179 goto error; 180 n -= nlen; 181 } 182 183 eh->ar_size = n; 184 185 if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL) 186 goto error; 187 188 eh->ar_flags = 0; 189 190 return (eh); 191 192 error: 193 if (eh) { 194 if (eh->ar_name) 195 free(eh->ar_name); 196 if (eh->ar_rawname) 197 free(eh->ar_rawname); 198 free(eh); 199 } 200 201 e->e_flags &= ~LIBELF_F_AR_HEADER; 202 e->e_hdr.e_rawhdr = (unsigned char *) arh; 203 204 return (NULL); 205 } 206 207 Elf * 208 _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf) 209 { 210 Elf *e; 211 off_t next; 212 size_t nsz, sz; 213 struct ar_hdr *arh; 214 char *member, *namelen; 215 216 assert(elf->e_kind == ELF_K_AR); 217 218 next = elf->e_u.e_ar.e_next; 219 220 /* 221 * `next' is only set to zero by elf_next() when the last 222 * member of an archive is processed. 223 */ 224 if (next == (off_t) 0) 225 return (NULL); 226 227 assert((next & 1) == 0); 228 229 arh = (struct ar_hdr *) (elf->e_rawfile + next); 230 231 /* 232 * Retrieve the size of the member. 233 */ 234 if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, 235 &sz) == 0) { 236 LIBELF_SET_ERROR(ARCHIVE, 0); 237 return (NULL); 238 } 239 240 /* 241 * Adjust the size field for members in BSD archives using 242 * extended naming. 243 */ 244 if (IS_EXTENDED_BSD_NAME(arh->ar_name)) { 245 namelen = arh->ar_name + 246 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE; 247 if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) - 248 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) { 249 LIBELF_SET_ERROR(ARCHIVE, 0); 250 return (NULL); 251 } 252 253 member = (char *) (arh + 1) + nsz; 254 sz -= nsz; 255 } else 256 member = (char *) (arh + 1); 257 258 259 if ((e = elf_memory(member, sz)) == NULL) 260 return (NULL); 261 262 e->e_fd = fd; 263 e->e_cmd = c; 264 e->e_hdr.e_rawhdr = (unsigned char *) arh; 265 266 elf->e_u.e_ar.e_nchildren++; 267 e->e_parent = elf; 268 269 return (e); 270 } 271 272 /* 273 * A BSD-style ar(1) symbol table has the following layout: 274 * 275 * - A count of bytes used by the following array of 'ranlib' 276 * structures, stored as a 'long'. 277 * - An array of 'ranlib' structures. Each array element is 278 * two 'long's in size. 279 * - A count of bytes used for the following symbol table. 280 * - The symbol table itself. 281 */ 282 283 /* 284 * A helper macro to read in a 'long' value from the archive. 285 * 286 * We use memcpy() since the source pointer may be misaligned with 287 * respect to the natural alignment for a C 'long'. 288 */ 289 #define GET_LONG(P, V)do { \ 290 memcpy(&(V), (P), sizeof(long)); \ 291 (P) += sizeof(long); \ 292 } while (/*CONSTCOND*/0) 293 294 Elf_Arsym * 295 _libelf_ar_process_bsd_symtab(Elf *e, size_t *count) 296 { 297 Elf_Arsym *symtab, *sym; 298 unsigned int n, nentries; 299 unsigned char *end, *p, *p0, *s, *s0; 300 const size_t entrysize = 2 * sizeof(long); 301 long arraysize, fileoffset, stroffset, strtabsize; 302 303 assert(e != NULL); 304 assert(count != NULL); 305 assert(e->e_u.e_ar.e_symtab == NULL); 306 307 symtab = NULL; 308 309 /* 310 * The BSD symbol table always contains the count fields even 311 * if there are no entries in it. 312 */ 313 if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long)) 314 goto symtaberror; 315 316 p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab; 317 end = p0 + e->e_u.e_ar.e_rawsymtabsz; 318 319 /* 320 * Retrieve the size of the array of ranlib descriptors and 321 * check it for validity. 322 */ 323 GET_LONG(p, arraysize); 324 325 if (arraysize < 0 || p0 + arraysize >= end || 326 ((size_t) arraysize % entrysize != 0)) 327 goto symtaberror; 328 329 /* 330 * Check the value of the string table size. 331 */ 332 s = p + arraysize; 333 GET_LONG(s, strtabsize); 334 335 s0 = s; /* Start of string table. */ 336 if (strtabsize < 0 || s0 + strtabsize > end) 337 goto symtaberror; 338 339 nentries = (size_t) arraysize / entrysize; 340 341 /* 342 * Allocate space for the returned Elf_Arsym array. 343 */ 344 if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) { 345 LIBELF_SET_ERROR(RESOURCE, 0); 346 return (NULL); 347 } 348 349 /* Read in symbol table entries. */ 350 for (n = 0, sym = symtab; n < nentries; n++, sym++) { 351 GET_LONG(p, stroffset); 352 GET_LONG(p, fileoffset); 353 354 if (stroffset < 0 || fileoffset < 0 || 355 (size_t) fileoffset >= e->e_rawsize) 356 goto symtaberror; 357 358 s = s0 + stroffset; 359 360 if (s >= end) 361 goto symtaberror; 362 363 sym->as_off = (off_t) fileoffset; 364 sym->as_hash = elf_hash((char *) s); 365 sym->as_name = (char *) s; 366 } 367 368 /* Fill up the sentinel entry. */ 369 sym->as_name = NULL; 370 sym->as_hash = ~0UL; 371 sym->as_off = (off_t) 0; 372 373 /* Remember the processed symbol table. */ 374 e->e_u.e_ar.e_symtab = symtab; 375 376 *count = e->e_u.e_ar.e_symtabsz = nentries + 1; 377 378 return (symtab); 379 380 symtaberror: 381 if (symtab) 382 free(symtab); 383 LIBELF_SET_ERROR(ARCHIVE, 0); 384 return (NULL); 385 } 386 387 /* 388 * An SVR4-style ar(1) symbol table has the following layout: 389 * 390 * - The first 4 bytes are a binary count of the number of entries in the 391 * symbol table, stored MSB-first. 392 * - Then there are 'n' 4-byte binary offsets, also stored MSB first. 393 * - Following this, there are 'n' null-terminated strings. 394 */ 395 396 #define GET_WORD(P, V) do { \ 397 (V) = 0; \ 398 (V) = (P)[0]; (V) <<= 8; \ 399 (V) += (P)[1]; (V) <<= 8; \ 400 (V) += (P)[2]; (V) <<= 8; \ 401 (V) += (P)[3]; \ 402 } while (0) 403 404 #define INTSZ 4 405 406 407 Elf_Arsym * 408 _libelf_ar_process_svr4_symtab(Elf *e, size_t *count) 409 { 410 uint32_t off; 411 size_t n, nentries; 412 Elf_Arsym *symtab, *sym; 413 unsigned char *p, *s, *end; 414 415 assert(e != NULL); 416 assert(count != NULL); 417 assert(e->e_u.e_ar.e_symtab == NULL); 418 419 symtab = NULL; 420 421 if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) 422 goto symtaberror; 423 424 p = (unsigned char *) e->e_u.e_ar.e_rawsymtab; 425 end = p + e->e_u.e_ar.e_rawsymtabsz; 426 427 GET_WORD(p, nentries); 428 p += INTSZ; 429 430 if (nentries == 0 || p + nentries * INTSZ >= end) 431 goto symtaberror; 432 433 /* Allocate space for a nentries + a sentinel. */ 434 if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) { 435 LIBELF_SET_ERROR(RESOURCE, 0); 436 return (NULL); 437 } 438 439 s = p + (nentries * INTSZ); /* start of the string table. */ 440 441 for (n = nentries, sym = symtab; n > 0; n--) { 442 if (s >= end) 443 goto symtaberror; 444 445 GET_WORD(p, off); 446 if (off >= e->e_rawsize) 447 goto symtaberror; 448 449 sym->as_off = (off_t) off; 450 sym->as_hash = elf_hash((char *) s); 451 sym->as_name = (char *) s; 452 453 p += INTSZ; 454 sym++; 455 456 for (; s < end && *s++ != '\0';) /* skip to next string */ 457 ; 458 } 459 460 /* Fill up the sentinel entry. */ 461 sym->as_name = NULL; 462 sym->as_hash = ~0UL; 463 sym->as_off = (off_t) 0; 464 465 *count = e->e_u.e_ar.e_symtabsz = nentries + 1; 466 e->e_u.e_ar.e_symtab = symtab; 467 468 return (symtab); 469 470 symtaberror: 471 if (symtab) 472 free(symtab); 473 LIBELF_SET_ERROR(ARCHIVE, 0); 474 return (NULL); 475 } 476