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