Print this page
10138 smatch fixes for usr/src/cmd/sgs
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/libelf/common/update.c
+++ new/usr/src/cmd/sgs/libelf/common/update.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
22 22 /*
23 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 28 * Copyright (c) 1988 AT&T
29 29 * All Rights Reserved
30 30 */
31 31
32 +/*
33 + * Copyright (c) 2018, Joyent, Inc.
34 + */
35 +
32 36 #include <memory.h>
33 37 #include <malloc.h>
34 38 #include <limits.h>
35 39
36 40 #include <sgs.h>
37 41 #include "decl.h"
38 42 #include "msg.h"
39 43
40 44 /*
41 45 * This module is compiled twice, the second time having
42 46 * -D_ELF64 defined. The following set of macros, along
43 47 * with machelf.h, represent the differences between the
44 48 * two compilations. Be careful *not* to add any class-
45 49 * dependent code (anything that has elf32 or elf64 in the
46 50 * name) to this code without hiding it behind a switch-
47 51 * able macro like these.
48 52 */
49 53 #if defined(_ELF64)
50 54
51 55 #define FSZ_LONG ELF64_FSZ_XWORD
52 56 #define ELFCLASS ELFCLASS64
53 57 #define _elf_snode_init _elf64_snode_init
54 58 #define _elfxx_cookscn _elf64_cookscn
55 59 #define _elf_upd_lib _elf64_upd_lib
56 60 #define elf_fsize elf64_fsize
57 61 #define _elf_entsz _elf64_entsz
58 62 #define _elf_msize _elf64_msize
59 63 #define _elf_upd_usr _elf64_upd_usr
60 64 #define wrt wrt64
61 65 #define elf_xlatetof elf64_xlatetof
62 66 #define _elfxx_update _elf64_update
63 67 #define _elfxx_swap_wrimage _elf64_swap_wrimage
64 68
65 69 #else /* ELF32 */
66 70
67 71 #define FSZ_LONG ELF32_FSZ_WORD
68 72 #define ELFCLASS ELFCLASS32
69 73 #define _elf_snode_init _elf32_snode_init
70 74 #define _elfxx_cookscn _elf32_cookscn
71 75 #define _elf_upd_lib _elf32_upd_lib
72 76 #define elf_fsize elf32_fsize
73 77 #define _elf_entsz _elf32_entsz
74 78 #define _elf_msize _elf32_msize
75 79 #define _elf_upd_usr _elf32_upd_usr
76 80 #define wrt wrt32
77 81 #define elf_xlatetof elf32_xlatetof
78 82 #define _elfxx_update _elf32_update
79 83 #define _elfxx_swap_wrimage _elf32_swap_wrimage
80 84
81 85 #endif /* ELF64 */
82 86
83 87
84 88 #if !(defined(_LP64) && defined(_ELF64))
85 89 #define TEST_SIZE
86 90
87 91 /*
88 92 * Handle the decision of whether the current linker can handle the
89 93 * desired object size, and if not, which error to issue.
90 94 *
91 95 * Input is the desired size. On failure, an error has been issued
92 96 * and 0 is returned. On success, 1 is returned.
93 97 */
94 98 static int
95 99 test_size(Lword hi)
96 100 {
97 101 #ifndef _LP64 /* 32-bit linker */
98 102 /*
99 103 * A 32-bit libelf is limited to a 2GB output file. This limit
100 104 * is due to the fact that off_t is a signed value, and that
101 105 * libelf cannot support large file support:
102 106 * - ABI reasons
103 107 * - Memory use generally is 2x output file size anyway,
104 108 * so lifting the file size limit will just send
105 109 * you crashing into the 32-bit VM limit.
106 110 * If the output is an ELFCLASS64 object, or an ELFCLASS32 object
107 111 * under 4GB, switching to the 64-bit version of libelf will help.
108 112 * However, an ELFCLASS32 object must not exceed 4GB.
109 113 */
110 114 if (hi > INT_MAX) { /* Bigger than 2GB */
111 115 #ifndef _ELF64
112 116 /* ELFCLASS32 object is fundamentally too big? */
113 117 if (hi > UINT_MAX) {
114 118 _elf_seterr(EFMT_FBIG_CLASS32, 0);
115 119 return (0);
116 120 }
117 121 #endif /* _ELF64 */
118 122
119 123 /* Should switch to the 64-bit libelf? */
120 124 _elf_seterr(EFMT_FBIG_LARGEFILE, 0);
121 125 return (0);
122 126 }
123 127 #endif /* !_LP64 */
124 128
125 129
126 130 #if defined(_LP64) && !defined(_ELF64) /* 64-bit linker, ELFCLASS32 */
127 131 /*
128 132 * A 64-bit linker can produce any size output
129 133 * file, but if the resulting file is ELFCLASS32,
130 134 * it must not exceed 4GB.
131 135 */
132 136 if (hi > UINT_MAX) {
133 137 _elf_seterr(EFMT_FBIG_CLASS32, 0);
134 138 return (0);
135 139 }
136 140 #endif
137 141
138 142 return (1);
139 143 }
140 144 #endif /* TEST_SIZE */
141 145
142 146 /*
143 147 * Output file update
144 148 * These functions walk an Elf structure, update its information,
145 149 * and optionally write the output file. Because the application
146 150 * may control of the output file layout, two upd_... routines
147 151 * exist. They're similar but too different to merge cleanly.
148 152 *
149 153 * The library defines a "dirty" bit to force parts of the file
150 154 * to be written on update. These routines ignore the dirty bit
151 155 * and do everything. A minimal update routine might be useful
152 156 * someday.
153 157 */
154 158
155 159 static size_t
156 160 _elf_upd_lib(Elf * elf)
157 161 {
158 162 Lword hi;
159 163 Lword hibit;
160 164 Elf_Scn * s;
161 165 register Lword sz;
162 166 Ehdr * eh = elf->ed_ehdr;
163 167 unsigned ver = eh->e_version;
164 168 register char *p = (char *)eh->e_ident;
165 169 size_t scncnt;
166 170
167 171 /*
168 172 * Ehdr and Phdr table go first
169 173 */
170 174 p[EI_MAG0] = ELFMAG0;
171 175 p[EI_MAG1] = ELFMAG1;
172 176 p[EI_MAG2] = ELFMAG2;
173 177 p[EI_MAG3] = ELFMAG3;
174 178 p[EI_CLASS] = ELFCLASS;
175 179 /* LINTED */
176 180 p[EI_VERSION] = (Byte)ver;
177 181 hi = elf_fsize(ELF_T_EHDR, 1, ver);
178 182 /* LINTED */
179 183 eh->e_ehsize = (Half)hi;
180 184 if (eh->e_phnum != 0) {
181 185 /* LINTED */
182 186 eh->e_phentsize = (Half)elf_fsize(ELF_T_PHDR, 1, ver);
183 187 /* LINTED */
184 188 eh->e_phoff = (Off)hi;
185 189 hi += eh->e_phentsize * eh->e_phnum;
186 190 } else {
187 191 eh->e_phoff = 0;
188 192 eh->e_phentsize = 0;
189 193 }
190 194
191 195 /*
192 196 * Obtain the first section header. Typically, this section has NULL
193 197 * contents, however in the case of Extended ELF Sections this section
194 198 * is used to hold an alternative e_shnum, e_shstrndx and e_phnum.
195 199 * On initial allocation (see _elf_snode) the elements of this section
196 200 * would have been zeroed. The e_shnum is initialized later, after the
197 201 * section header count has been determined. The e_shstrndx and
198 202 * e_phnum may have already been initialized by the caller (for example,
199 203 * gelf_update_shdr() in mcs(1)).
200 204 */
201 205 if ((s = elf->ed_hdscn) == 0) {
202 206 eh->e_shnum = 0;
203 207 scncnt = 0;
204 208 } else {
205 209 s = s->s_next;
206 210 scncnt = 1;
207 211 }
208 212
209 213 /*
210 214 * Loop through sections. Compute section size before changing hi.
211 215 * Allow null buffers for NOBITS.
212 216 */
213 217 hibit = 0;
214 218 for (; s != 0; s = s->s_next) {
215 219 register Dnode *d;
216 220 register Lword fsz, j;
217 221 Shdr *sh = s->s_shdr;
218 222
219 223 scncnt++;
220 224 if (sh->sh_type == SHT_NULL) {
221 225 *sh = _elf_snode_init.sb_shdr;
222 226 continue;
223 227 }
224 228
225 229 if ((s->s_myflags & SF_READY) == 0)
226 230 (void) _elfxx_cookscn(s);
227 231
228 232 sh->sh_addralign = 1;
229 233 if ((sz = (Lword)_elf_entsz(elf, sh->sh_type, ver)) != 0)
230 234 /* LINTED */
231 235 sh->sh_entsize = (Half)sz;
232 236 sz = 0;
233 237 for (d = s->s_hdnode; d != 0; d = d->db_next) {
234 238 if ((fsz = elf_fsize(d->db_data.d_type,
235 239 1, ver)) == 0)
236 240 return (0);
237 241
238 242 j = _elf_msize(d->db_data.d_type, ver);
239 243 fsz *= (d->db_data.d_size / j);
240 244 d->db_osz = (size_t)fsz;
241 245 if ((j = d->db_data.d_align) > 1) {
242 246 if (j > sh->sh_addralign)
243 247 sh->sh_addralign = (Xword)j;
244 248
245 249 if (sz % j != 0)
246 250 sz += j - sz % j;
247 251 }
248 252 d->db_data.d_off = (off_t)sz;
249 253 d->db_xoff = sz;
250 254 sz += fsz;
251 255 }
252 256
253 257 sh->sh_size = (Xword) sz;
254 258 /*
255 259 * We want to take into account the offsets for NOBITS
256 260 * sections and let the "sh_offsets" point to where
257 261 * the section would 'conceptually' fit within
258 262 * the file (as required by the ABI).
259 263 *
260 264 * But - we must also make sure that the NOBITS does
261 265 * not take up any actual space in the file. We preserve
262 266 * the actual offset into the file in the 'hibit' variable.
263 267 * When we come to the first non-NOBITS section after a
264 268 * encountering a NOBITS section the hi counter is restored
265 269 * to its proper place in the file.
266 270 */
267 271 if (sh->sh_type == SHT_NOBITS) {
268 272 if (hibit == 0)
269 273 hibit = hi;
270 274 } else {
271 275 if (hibit) {
272 276 hi = hibit;
273 277 hibit = 0;
274 278 }
275 279 }
276 280 j = sh->sh_addralign;
277 281 if ((fsz = hi % j) != 0)
278 282 hi += j - fsz;
279 283
280 284 /* LINTED */
281 285 sh->sh_offset = (Off)hi;
282 286 hi += sz;
283 287 }
284 288
285 289 /*
286 290 * if last section was a 'NOBITS' section then we need to
287 291 * restore the 'hi' counter to point to the end of the last
288 292 * non 'NOBITS' section.
289 293 */
290 294 if (hibit) {
291 295 hi = hibit;
292 296 hibit = 0;
293 297 }
294 298
295 299 /*
296 300 * Shdr table last
297 301 */
298 302 if (scncnt != 0) {
299 303 if (hi % FSZ_LONG != 0)
300 304 hi += FSZ_LONG - hi % FSZ_LONG;
301 305 /* LINTED */
302 306 eh->e_shoff = (Off)hi;
303 307 /*
304 308 * If we are using 'extended sections' then the
305 309 * e_shnum is stored in the sh_size field of the
306 310 * first section header.
307 311 *
308 312 * NOTE: we set e_shnum to '0' because it's specified
309 313 * this way in the gABI, and in the hopes that
310 314 * this will cause less problems to unaware
311 315 * tools then if we'd set it to SHN_XINDEX (0xffff).
312 316 */
313 317 if (scncnt < SHN_LORESERVE)
314 318 eh->e_shnum = scncnt;
315 319 else {
316 320 Shdr *sh;
317 321 sh = (Shdr *)elf->ed_hdscn->s_shdr;
318 322 sh->sh_size = scncnt;
319 323 eh->e_shnum = 0;
320 324 }
321 325 /* LINTED */
322 326 eh->e_shentsize = (Half)elf_fsize(ELF_T_SHDR, 1, ver);
323 327 hi += eh->e_shentsize * scncnt;
324 328 } else {
325 329 eh->e_shoff = 0;
326 330 eh->e_shentsize = 0;
327 331 }
328 332
329 333 #ifdef TEST_SIZE
330 334 if (test_size(hi) == 0)
331 335 return (0);
332 336 #endif
333 337
334 338 return ((size_t)hi);
335 339 }
336 340
337 341
338 342
339 343 static size_t
340 344 _elf_upd_usr(Elf * elf)
341 345 {
342 346 Lword hi;
343 347 Elf_Scn * s;
344 348 register Lword sz;
345 349 Ehdr * eh = elf->ed_ehdr;
346 350 unsigned ver = eh->e_version;
347 351 register char *p = (char *)eh->e_ident;
348 352 size_t scncnt;
349 353
350 354 /*
351 355 * Ehdr and Phdr table go first
352 356 */
353 357 p[EI_MAG0] = ELFMAG0;
354 358 p[EI_MAG1] = ELFMAG1;
355 359 p[EI_MAG2] = ELFMAG2;
356 360 p[EI_MAG3] = ELFMAG3;
357 361 p[EI_CLASS] = ELFCLASS;
358 362 /* LINTED */
359 363 p[EI_VERSION] = (Byte)ver;
360 364 hi = elf_fsize(ELF_T_EHDR, 1, ver);
361 365 /* LINTED */
362 366 eh->e_ehsize = (Half)hi;
363 367
364 368 /*
365 369 * If phnum is zero, phoff "should" be zero too,
366 370 * but the application is responsible for it.
367 371 * Allow a non-zero value here and update the
368 372 * hi water mark accordingly.
369 373 */
370 374
371 375 if (eh->e_phnum != 0)
372 376 /* LINTED */
373 377 eh->e_phentsize = (Half)elf_fsize(ELF_T_PHDR, 1, ver);
374 378 else
375 379 eh->e_phentsize = 0;
376 380 if ((sz = eh->e_phoff + eh->e_phentsize * eh->e_phnum) > hi)
377 381 hi = sz;
378 382
379 383 /*
380 384 * Loop through sections, skipping index zero.
381 385 * Compute section size before changing hi.
382 386 * Allow null buffers for NOBITS.
383 387 */
384 388
385 389 if ((s = elf->ed_hdscn) == 0) {
386 390 eh->e_shnum = 0;
387 391 scncnt = 0;
388 392 } else {
389 393 scncnt = 1;
390 394 s = s->s_next;
391 395 }
392 396 for (; s != 0; s = s->s_next) {
393 397 register Dnode *d;
394 398 register Lword fsz, j;
395 399 Shdr *sh = s->s_shdr;
396 400
397 401 if ((s->s_myflags & SF_READY) == 0)
398 402 (void) _elfxx_cookscn(s);
399 403
400 404 ++scncnt;
401 405 sz = 0;
402 406 for (d = s->s_hdnode; d != 0; d = d->db_next) {
403 407 if ((fsz = elf_fsize(d->db_data.d_type, 1,
404 408 ver)) == 0)
405 409 return (0);
406 410 j = _elf_msize(d->db_data.d_type, ver);
407 411 fsz *= (d->db_data.d_size / j);
408 412 d->db_osz = (size_t)fsz;
409 413
410 414 if ((sh->sh_type != SHT_NOBITS) &&
411 415 ((j = (d->db_data.d_off + d->db_osz)) > sz))
412 416 sz = j;
413 417 }
414 418 if (sh->sh_size < sz) {
415 419 _elf_seterr(EFMT_SCNSZ, 0);
416 420 return (0);
417 421 }
418 422 if ((sh->sh_type != SHT_NOBITS) &&
419 423 (hi < sh->sh_offset + sh->sh_size))
420 424 hi = sh->sh_offset + sh->sh_size;
421 425 }
422 426
423 427 /*
424 428 * Shdr table last. Comment above for phnum/phoff applies here.
425 429 */
426 430 if (scncnt != 0) {
427 431 /* LINTED */
428 432 eh->e_shentsize = (Half)elf_fsize(ELF_T_SHDR, 1, ver);
429 433 if (scncnt < SHN_LORESERVE) {
430 434 eh->e_shnum = scncnt;
431 435 } else {
432 436 Shdr *sh;
433 437 sh = (Shdr *)elf->ed_hdscn->s_shdr;
434 438 sh->sh_size = scncnt;
435 439 eh->e_shnum = 0;
436 440 }
437 441 } else {
438 442 eh->e_shentsize = 0;
439 443 }
440 444
441 445 if ((sz = eh->e_shoff + eh->e_shentsize * scncnt) > hi)
442 446 hi = sz;
443 447
444 448 #ifdef TEST_SIZE
445 449 if (test_size(hi) == 0)
446 450 return (0);
447 451 #endif
448 452
449 453 return ((size_t)hi);
450 454 }
451 455
452 456
453 457 static size_t
454 458 wrt(Elf * elf, Xword outsz, unsigned fill, int update_cmd)
455 459 {
456 460 Elf_Data dst, src;
457 461 unsigned flag;
458 462 Xword hi, sz;
459 463 char *image;
460 464 Elf_Scn *s;
461 465 Ehdr *eh = elf->ed_ehdr;
462 466 unsigned ver = eh->e_version;
463 467 unsigned encode;
464 468 int byte;
465 469 _elf_execfill_func_t *execfill_func;
466 470
467 471 /*
468 472 * If this is an ELF_C_WRIMAGE write, then we encode into the
469 473 * byte order of the system we are running on rather than that of
470 474 * of the object. For ld.so.1, this is the same order, but
471 475 * for 'ld', it might not be in the case where we are cross
472 476 * linking an object for a different target. In this later case,
473 477 * the linker-host byte order is necessary so that the linker can
474 478 * manipulate the resulting image. It is expected that the linker
475 479 * will call elf_swap_wrimage() if necessary to convert the image
476 480 * to the target byte order.
477 481 */
478 482 encode = (update_cmd == ELF_C_WRIMAGE) ? _elf_sys_encoding() :
479 483 eh->e_ident[EI_DATA];
480 484
481 485 /*
482 486 * Two issues can cause trouble for the output file.
483 487 * First, begin() with ELF_C_RDWR opens a file for both
484 488 * read and write. On the write update(), the library
485 489 * has to read everything it needs before truncating
486 490 * the file. Second, using mmap for both read and write
487 491 * is too tricky. Consequently, the library disables mmap
488 492 * on the read side. Using mmap for the output saves swap
489 493 * space, because that mapping is SHARED, not PRIVATE.
490 494 *
491 495 * If the file is write-only, there can be nothing of
492 496 * interest to bother with.
493 497 *
494 498 * The following reads the entire file, which might be
495 499 * more than necessary. Better safe than sorry.
496 500 */
497 501
498 502 if ((elf->ed_myflags & EDF_READ) &&
499 503 (_elf_vm(elf, (size_t)0, elf->ed_fsz) != OK_YES))
500 504 return (0);
501 505
502 506 flag = elf->ed_myflags & EDF_WRALLOC;
503 507 if ((image = _elf_outmap(elf->ed_fd, outsz, &flag)) == 0)
504 508 return (0);
505 509
506 510 if (flag == 0)
507 511 elf->ed_myflags |= EDF_IMALLOC;
508 512
509 513 /*
510 514 * If an error occurs below, a "dirty" bit may be cleared
511 515 * improperly. To save a second pass through the file,
512 516 * this code sets the dirty bit on the elf descriptor
513 517 * when an error happens, assuming that will "cover" any
514 518 * accidents.
515 519 */
516 520
517 521 /*
518 522 * Hi is needed only when 'fill' is non-zero.
519 523 * Fill is non-zero only when the library
520 524 * calculates file/section/data buffer offsets.
521 525 * The lib guarantees they increase monotonically.
522 526 * That guarantees proper filling below.
523 527 */
524 528
525 529
526 530 /*
527 531 * Ehdr first
528 532 */
529 533
530 534 src.d_buf = (Elf_Void *)eh;
531 535 src.d_type = ELF_T_EHDR;
532 536 src.d_size = sizeof (Ehdr);
533 537 src.d_version = EV_CURRENT;
534 538 dst.d_buf = (Elf_Void *)image;
535 539 dst.d_size = eh->e_ehsize;
536 540 dst.d_version = ver;
537 541 if (elf_xlatetof(&dst, &src, encode) == 0)
538 542 return (0);
539 543 elf->ed_ehflags &= ~ELF_F_DIRTY;
540 544 hi = eh->e_ehsize;
541 545
542 546 /*
543 547 * Phdr table if one exists
544 548 */
545 549
546 550 if (eh->e_phnum != 0) {
547 551 unsigned work;
548 552 /*
549 553 * Unlike other library data, phdr table is
550 554 * in the user version. Change src buffer
551 555 * version here, fix it after translation.
552 556 */
553 557
554 558 src.d_buf = (Elf_Void *)elf->ed_phdr;
555 559 src.d_type = ELF_T_PHDR;
556 560 src.d_size = elf->ed_phdrsz;
557 561 ELFACCESSDATA(work, _elf_work)
558 562 src.d_version = work;
559 563 dst.d_buf = (Elf_Void *)(image + eh->e_phoff);
560 564 dst.d_size = eh->e_phnum * eh->e_phentsize;
561 565 hi = (Xword)(eh->e_phoff + dst.d_size);
562 566 if (elf_xlatetof(&dst, &src, encode) == 0) {
563 567 elf->ed_uflags |= ELF_F_DIRTY;
564 568 return (0);
565 569 }
566 570 elf->ed_phflags &= ~ELF_F_DIRTY;
567 571 src.d_version = EV_CURRENT;
568 572 }
569 573
570 574 /*
571 575 * Loop through sections
572 576 */
573 577
574 578 ELFACCESSDATA(byte, _elf_byte);
575 579 ELFACCESSDATA(execfill_func, _elf_execfill_func);
576 580 for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
577 581 register Dnode *d, *prevd;
578 582 Xword off = 0;
579 583 Shdr *sh = s->s_shdr;
580 584 char *start = image + sh->sh_offset;
581 585 char *here;
582 586 _elf_execfill_func_t *execfill;
583 587
584 588 /* Only use the execfill function on SHF_EXECINSTR sections */
585 589 execfill = (sh->sh_flags & SHF_EXECINSTR) ?
586 590 execfill_func : NULL;
587 591
588 592 /*
589 593 * Just "clean" DIRTY flag for "empty" sections. Even if
590 594 * NOBITS needs padding, the next thing in the
591 595 * file will provide it. (And if this NOBITS is
592 596 * the last thing in the file, no padding needed.)
593 597 */
594 598 if ((sh->sh_type == SHT_NOBITS) ||
595 599 (sh->sh_type == SHT_NULL)) {
596 600 d = s->s_hdnode, prevd = 0;
597 601 for (; d != 0; prevd = d, d = d->db_next)
598 602 d->db_uflags &= ~ELF_F_DIRTY;
599 603 continue;
600 604 }
601 605 /*
602 606 * Clear out the memory between the end of the last
603 607 * section and the begining of this section.
604 608 */
605 609 if (fill && (sh->sh_offset > hi)) {
606 610 sz = sh->sh_offset - hi;
607 611 (void) memset(start - sz, byte, sz);
608 612 }
609 613
610 614
611 615 for (d = s->s_hdnode, prevd = 0;
612 616 d != 0; prevd = d, d = d->db_next) {
613 617 d->db_uflags &= ~ELF_F_DIRTY;
614 618 here = start + d->db_data.d_off;
615 619
616 620 /*
617 621 * Clear out the memory between the end of the
618 622 * last update and the start of this data buffer.
619 623 *
620 624 * These buffers represent input sections that have
621 625 * been concatenated into an output section, so if
622 626 * the output section is executable (SHF_EXECINSTR)
623 627 * and a fill function has been registered, use the
624 628 * function. Otherwise, use the fill byte.
625 629 */
626 630 if (fill && (d->db_data.d_off > off)) {
627 631 sz = (Xword)(d->db_data.d_off - off);
628 632 if (execfill != NULL)
629 633 (* execfill)(start,
630 634 here - start - sz, sz);
631 635 else
632 636 (void) memset(here - sz, byte, sz);
633 637 }
634 638
635 639 if ((d->db_myflags & DBF_READY) == 0) {
636 640 SCNLOCK(s);
637 641 if (_elf_locked_getdata(s, &prevd->db_data) !=
638 642 &d->db_data) {
639 643 elf->ed_uflags |= ELF_F_DIRTY;
640 644 SCNUNLOCK(s);
641 645 return (0);
642 646 }
643 647 SCNUNLOCK(s);
644 648 }
645 649 dst.d_buf = (Elf_Void *)here;
646 650 dst.d_size = d->db_osz;
647 651
648 652 /*
649 653 * Copy the translated bits out to the destination
650 654 * image.
651 655 */
652 656 if (elf_xlatetof(&dst, &d->db_data, encode) == 0) {
653 657 elf->ed_uflags |= ELF_F_DIRTY;
654 658 return (0);
655 659 }
656 660
657 661 off = (Xword)(d->db_data.d_off + dst.d_size);
658 662 }
659 663 hi = sh->sh_offset + sh->sh_size;
660 664 }
661 665
662 666 /*
663 667 * Shdr table last
664 668 */
665 669
666 670 if (fill && (eh->e_shoff > hi)) {
667 671 sz = eh->e_shoff - hi;
668 672 (void) memset(image + hi, byte, sz);
669 673 }
670 674
671 675 src.d_type = ELF_T_SHDR;
672 676 src.d_size = sizeof (Shdr);
673 677 dst.d_buf = (Elf_Void *)(image + eh->e_shoff);
674 678 dst.d_size = eh->e_shentsize;
675 679 for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
676 680 assert((uintptr_t)dst.d_buf < ((uintptr_t)image + outsz));
677 681 s->s_shflags &= ~ELF_F_DIRTY;
678 682 s->s_uflags &= ~ELF_F_DIRTY;
679 683 src.d_buf = s->s_shdr;
680 684
681 685 if (elf_xlatetof(&dst, &src, encode) == 0) {
682 686 elf->ed_uflags |= ELF_F_DIRTY;
683 687 return (0);
684 688 }
685 689
686 690 dst.d_buf = (char *)dst.d_buf + eh->e_shentsize;
687 691 }
688 692 /*
689 693 * ELF_C_WRIMAGE signifyes that we build the memory image, but
690 694 * that we do not actually write it to disk. This is used
691 695 * by ld(1) to build up a full image of an elf file and then
692 696 * to process the file before it's actually written out to
693 697 * disk. This saves ld(1) the overhead of having to write
694 698 * the image out to disk twice.
695 699 */
696 700 if (update_cmd == ELF_C_WRIMAGE) {
697 701 elf->ed_uflags &= ~ELF_F_DIRTY;
698 702 elf->ed_wrimage = image;
699 703 elf->ed_wrimagesz = outsz;
700 704 return (outsz);
701 705 }
702 706
703 707 if (_elf_outsync(elf->ed_fd, image, outsz,
704 708 ((elf->ed_myflags & EDF_IMALLOC) ? 0 : 1)) != 0) {
705 709 elf->ed_uflags &= ~ELF_F_DIRTY;
706 710 elf->ed_myflags &= ~EDF_IMALLOC;
707 711 return (outsz);
708 712 }
709 713
710 714 elf->ed_uflags |= ELF_F_DIRTY;
711 715 return (0);
712 716 }
713 717
714 718
715 719
716 720
717 721 /*
718 722 * The following is a private interface between the linkers (ld & ld.so.1)
719 723 * and libelf:
720 724 *
721 725 * elf_update(elf, ELF_C_WRIMAGE)
722 726 * This will cause full image representing the elf file
723 727 * described by the elf pointer to be built in memory. If the
724 728 * elf pointer has a valid file descriptor associated with it
725 729 * we will attempt to build the memory image from mmap()'ed
726 730 * storage. If the elf descriptor does not have a valid
727 731 * file descriptor (opened with elf_begin(0, ELF_C_IMAGE, 0))
728 732 * then the image will be allocated from dynamic memory (malloc()).
729 733 *
730 734 * elf_update() will return the size of the memory image built
731 735 * when sucessful.
732 736 *
733 737 * When a subsequent call to elf_update() with ELF_C_WRITE as
734 738 * the command is performed it will sync the image created
735 739 * by ELF_C_WRIMAGE to disk (if fd available) and
↓ open down ↓ |
694 lines elided |
↑ open up ↑ |
736 740 * free the memory allocated.
737 741 */
738 742
739 743 off_t
740 744 _elfxx_update(Elf * elf, Elf_Cmd cmd)
741 745 {
742 746 size_t sz;
743 747 unsigned u;
744 748 Ehdr *eh = elf->ed_ehdr;
745 749
746 - if (elf == 0)
747 - return (-1);
748 -
749 750 ELFWLOCK(elf)
750 751 switch (cmd) {
751 752 default:
752 753 _elf_seterr(EREQ_UPDATE, 0);
753 754 ELFUNLOCK(elf)
754 755 return (-1);
755 756
756 757 case ELF_C_WRIMAGE:
757 758 if ((elf->ed_myflags & EDF_WRITE) == 0) {
758 759 _elf_seterr(EREQ_UPDWRT, 0);
759 760 ELFUNLOCK(elf)
760 761 return (-1);
761 762 }
762 763 break;
763 764 case ELF_C_WRITE:
764 765 if ((elf->ed_myflags & EDF_WRITE) == 0) {
765 766 _elf_seterr(EREQ_UPDWRT, 0);
766 767 ELFUNLOCK(elf)
767 768 return (-1);
768 769 }
769 770 if (elf->ed_wrimage) {
770 771 if (elf->ed_myflags & EDF_WRALLOC) {
771 772 free(elf->ed_wrimage);
772 773 /*
773 774 * The size is still returned even
774 775 * though nothing is actually written
775 776 * out. This is just to be consistant
776 777 * with the rest of the interface.
777 778 */
778 779 sz = elf->ed_wrimagesz;
779 780 elf->ed_wrimage = 0;
780 781 elf->ed_wrimagesz = 0;
781 782 ELFUNLOCK(elf);
782 783 return ((off_t)sz);
783 784 }
784 785 sz = _elf_outsync(elf->ed_fd, elf->ed_wrimage,
785 786 elf->ed_wrimagesz,
786 787 (elf->ed_myflags & EDF_IMALLOC ? 0 : 1));
787 788 elf->ed_myflags &= ~EDF_IMALLOC;
788 789 elf->ed_wrimage = 0;
789 790 elf->ed_wrimagesz = 0;
790 791 ELFUNLOCK(elf);
791 792 return ((off_t)sz);
792 793 }
793 794 /* FALLTHROUGH */
794 795 case ELF_C_NULL:
795 796 break;
796 797 }
797 798
798 799 if (eh == 0) {
799 800 _elf_seterr(ESEQ_EHDR, 0);
800 801 ELFUNLOCK(elf)
801 802 return (-1);
802 803 }
803 804
804 805 if ((u = eh->e_version) > EV_CURRENT) {
805 806 _elf_seterr(EREQ_VER, 0);
806 807 ELFUNLOCK(elf)
807 808 return (-1);
808 809 }
809 810
810 811 if (u == EV_NONE)
811 812 eh->e_version = EV_CURRENT;
812 813
813 814 if ((u = eh->e_ident[EI_DATA]) == ELFDATANONE) {
814 815 unsigned encode;
815 816
816 817 ELFACCESSDATA(encode, _elf_encode)
817 818 if (encode == ELFDATANONE) {
818 819 _elf_seterr(EREQ_ENCODE, 0);
819 820 ELFUNLOCK(elf)
820 821 return (-1);
821 822 }
822 823 /* LINTED */
823 824 eh->e_ident[EI_DATA] = (Byte)encode;
824 825 }
825 826
826 827 u = 1;
827 828 if (elf->ed_uflags & ELF_F_LAYOUT) {
828 829 sz = _elf_upd_usr(elf);
829 830 u = 0;
830 831 } else
831 832 sz = _elf_upd_lib(elf);
832 833
833 834 if ((sz != 0) && ((cmd == ELF_C_WRITE) || (cmd == ELF_C_WRIMAGE)))
834 835 sz = wrt(elf, (Xword)sz, u, cmd);
835 836
836 837 if (sz == 0) {
837 838 ELFUNLOCK(elf)
838 839 return (-1);
839 840 }
840 841
841 842 ELFUNLOCK(elf)
842 843 return ((off_t)sz);
843 844 }
844 845
845 846
846 847 /*
847 848 * When wrt() processes an ELF_C_WRIMAGE request, the resulting image
848 849 * gets the byte order (encoding) of the platform running the linker
849 850 * rather than that of the target host. This allows the linker to modify
850 851 * the image, prior to flushing it to the output file. This routine
851 852 * is used to re-translate such an image into the byte order of the
852 853 * target host.
853 854 */
854 855 int
855 856 _elfxx_swap_wrimage(Elf *elf)
856 857 {
857 858 Elf_Data dst, src;
858 859 Elf_Scn *s;
859 860 Ehdr *eh;
860 861 Half e_phnum;
861 862 unsigned ver;
862 863 unsigned encode;
863 864
864 865 /*
865 866 * Ehdr first
866 867 */
867 868
868 869 ELFWLOCK(elf);
869 870 eh = elf->ed_ehdr;
870 871 e_phnum = eh->e_phnum;
871 872 ver = eh->e_version;
872 873 encode = eh->e_ident[EI_DATA];
873 874
874 875 src.d_buf = dst.d_buf = (Elf_Void *)eh;
875 876 src.d_type = dst.d_type = ELF_T_EHDR;
876 877 src.d_size = dst.d_size = sizeof (Ehdr);
877 878 src.d_version = dst.d_version = ver;
878 879 if (elf_xlatetof(&dst, &src, encode) == 0) {
879 880 ELFUNLOCK(elf);
880 881 return (1);
881 882 }
882 883
883 884 /*
884 885 * Phdr table if one exists
885 886 */
886 887
887 888 if (e_phnum != 0) {
888 889 unsigned work;
889 890 /*
890 891 * Unlike other library data, phdr table is
891 892 * in the user version.
892 893 */
893 894
894 895 src.d_buf = dst.d_buf = (Elf_Void *)elf->ed_phdr;
895 896 src.d_type = dst.d_type = ELF_T_PHDR;
896 897 src.d_size = dst.d_size = elf->ed_phdrsz;
897 898 ELFACCESSDATA(work, _elf_work)
898 899 src.d_version = dst.d_version = work;
899 900 if (elf_xlatetof(&dst, &src, encode) == 0) {
900 901 ELFUNLOCK(elf);
901 902 return (1);
902 903 }
903 904 }
904 905
905 906 /*
906 907 * Loop through sections
907 908 */
908 909
909 910 for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
910 911 register Dnode *d, *prevd;
911 912 Shdr *sh = s->s_shdr;
912 913
913 914 if ((sh->sh_type == SHT_NOBITS) || (sh->sh_type == SHT_NULL))
914 915 continue;
915 916
916 917 for (d = s->s_hdnode, prevd = 0;
917 918 d != 0; prevd = d, d = d->db_next) {
918 919
919 920 if ((d->db_myflags & DBF_READY) == 0) {
920 921 SCNLOCK(s);
921 922 if (_elf_locked_getdata(s, &prevd->db_data) !=
922 923 &d->db_data) {
923 924 SCNUNLOCK(s);
924 925 ELFUNLOCK(elf);
925 926 return (1);
926 927 }
927 928 SCNUNLOCK(s);
928 929 }
929 930
930 931 dst = d->db_data;
931 932 if (elf_xlatetof(&dst, &d->db_data, encode) == 0) {
932 933 ELFUNLOCK(elf);
933 934 return (1);
934 935 }
935 936 }
936 937 }
937 938
938 939 /*
939 940 * Shdr table
940 941 */
941 942
942 943 src.d_type = dst.d_type = ELF_T_SHDR;
943 944 src.d_version = dst.d_version = ver;
944 945 for (s = elf->ed_hdscn; s != 0; s = s->s_next) {
945 946 src.d_buf = dst.d_buf = s->s_shdr;
946 947 src.d_size = dst.d_size = sizeof (Shdr);
947 948 if (elf_xlatetof(&dst, &src, encode) == 0) {
948 949 ELFUNLOCK(elf);
949 950 return (1);
950 951 }
951 952 }
952 953
953 954 ELFUNLOCK(elf);
954 955 return (0);
955 956 }
956 957
957 958
958 959
959 960 #ifndef _ELF64
960 961 /* class-independent, only needs to be compiled once */
961 962
962 963 off_t
963 964 elf_update(Elf *elf, Elf_Cmd cmd)
964 965 {
965 966 if (elf == 0)
966 967 return (-1);
967 968
968 969 if (elf->ed_class == ELFCLASS32)
969 970 return (_elf32_update(elf, cmd));
970 971 else if (elf->ed_class == ELFCLASS64) {
971 972 return (_elf64_update(elf, cmd));
972 973 }
973 974
974 975 _elf_seterr(EREQ_CLASS, 0);
975 976 return (-1);
976 977 }
977 978
978 979 int
979 980 _elf_swap_wrimage(Elf *elf)
980 981 {
981 982 if (elf == 0)
982 983 return (0);
983 984
984 985 if (elf->ed_class == ELFCLASS32)
985 986 return (_elf32_swap_wrimage(elf));
986 987
987 988 if (elf->ed_class == ELFCLASS64)
988 989 return (_elf64_swap_wrimage(elf));
989 990
990 991 _elf_seterr(EREQ_CLASS, 0);
991 992 return (0);
992 993 }
993 994
994 995 /*
995 996 * 4106312, 4106398, This is an ad-hoc means for the 32-bit
996 997 * Elf64 version of libld.so.3 to get around the limitation
997 998 * of a 32-bit d_off field. This is only intended to be
998 999 * used by libld to relocate symbols in large NOBITS sections.
999 1000 */
1000 1001 Elf64_Off
1001 1002 _elf_getxoff(Elf_Data * d)
1002 1003 {
1003 1004 return (((Dnode *)d)->db_xoff);
1004 1005 }
1005 1006 #endif /* !_ELF64 */
↓ open down ↓ |
247 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX