Print this page
uts: Allow for address space randomisation.
Randomise the base addresses of shared objects, non-fixed mappings, the
stack and the heap. Introduce a service, svc:/system/process-security,
and a tool psecflags(1) to control and observe it
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/libld/common/update.c
+++ new/usr/src/cmd/sgs/libld/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 (c) 1988 AT&T
24 24 * All Rights Reserved
25 25 *
26 26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 27 */
28 28
29 29 /*
30 30 * Update the new output file image, perform virtual address, offset and
31 31 * displacement calculations on the program headers and sections headers,
32 32 * and generate any new output section information.
33 33 */
34 34
35 35 #define ELF_TARGET_AMD64
36 36
37 37 #include <stdio.h>
38 38 #include <string.h>
39 39 #include <unistd.h>
40 40 #include <debug.h>
41 41 #include "msg.h"
42 42 #include "_libld.h"
43 43
44 44 /*
45 45 * Comparison routine used by qsort() for sorting of the global symbol list
46 46 * based off of the hashbuckets the symbol will eventually be deposited in.
47 47 */
48 48 static int
49 49 sym_hash_compare(Sym_s_list * s1, Sym_s_list * s2)
50 50 {
51 51 return (s1->sl_hval - s2->sl_hval);
52 52 }
53 53
54 54 /*
55 55 * Comparison routine used by qsort() for sorting of dyn[sym|tls]sort section
56 56 * indices based on the address of the symbols they reference. The
57 57 * use of the global dynsort_compare_syms variable is needed because
58 58 * we need to examine the symbols the indices reference. It is safe, because
59 59 * the linker is single threaded.
60 60 */
61 61 Sym *dynsort_compare_syms;
62 62
63 63 static int
64 64 dynsort_compare(const void *idx1, const void *idx2)
65 65 {
66 66 Sym *s1 = dynsort_compare_syms + *((const Word *) idx1);
67 67 Sym *s2 = dynsort_compare_syms + *((const Word *) idx2);
68 68
69 69 /*
70 70 * Note: the logical computation for this is
71 71 * (st_value1 - st_value2)
72 72 * However, that is only correct if the address type is smaller
73 73 * than a pointer. Writing it this way makes it immune to the
74 74 * class (32 or 64-bit) of the linker.
75 75 */
76 76 return ((s1->st_value < s2->st_value) ? -1 :
77 77 (s1->st_value > s2->st_value));
78 78 }
79 79
80 80 /*
81 81 * Scan the sorted symbols, and issue warnings if there are any duplicate
82 82 * values in the list. We only do this if -zverbose is set, or we are
83 83 * running with LD_DEBUG defined
84 84 *
85 85 * entry:
86 86 * ofl - Output file descriptor
87 87 * ldynsym - Pointer to start of .SUNW_ldynsym section that the
88 88 * sort section indexes reference.
89 89 * symsort - Pointer to start of .SUNW_dynsymsort or .SUNW_dyntlssort
90 90 * section.
91 91 * n - # of indices in symsort array
92 92 * secname - Name of the symsort section.
93 93 *
94 94 * exit:
95 95 * If the symsort section contains indexes to more than one
96 96 * symbol with the same address value, a warning is issued.
97 97 */
98 98 static void
99 99 dynsort_dupwarn(Ofl_desc *ofl, Sym *ldynsym, const char *str,
100 100 Word *symsort, Word n, const char *secname)
101 101 {
102 102 int zverbose = (ofl->ofl_flags & FLG_OF_VERBOSE) != 0;
103 103 Word ndx, cmp_ndx;
104 104 Addr addr, cmp_addr;
105 105
106 106 /* Nothing to do if -zverbose or LD_DEBUG are not active */
107 107 if (!(zverbose || DBG_ENABLED))
108 108 return;
109 109
110 110 cmp_ndx = 0;
111 111 cmp_addr = ldynsym[symsort[cmp_ndx]].st_value;
112 112 for (ndx = 1; ndx < n; ndx++) {
113 113 addr = ldynsym[symsort[ndx]].st_value;
114 114 if (cmp_addr == addr) {
115 115 if (zverbose)
116 116 ld_eprintf(ofl, ERR_WARNING,
117 117 MSG_INTL(MSG_SYM_DUPSORTADDR), secname,
118 118 str + ldynsym[symsort[cmp_ndx]].st_name,
119 119 str + ldynsym[symsort[ndx]].st_name,
120 120 EC_ADDR(addr));
121 121 DBG_CALL(Dbg_syms_dup_sort_addr(ofl->ofl_lml, secname,
122 122 str + ldynsym[symsort[cmp_ndx]].st_name,
123 123 str + ldynsym[symsort[ndx]].st_name,
124 124 EC_ADDR(addr)));
125 125 } else { /* Not a dup. Move reference up */
126 126 cmp_ndx = ndx;
127 127 cmp_addr = addr;
128 128 }
129 129 }
130 130 }
131 131
132 132 /*
133 133 * Build and update any output symbol tables. Here we work on all the symbol
134 134 * tables at once to reduce the duplication of symbol and string manipulation.
135 135 * Symbols and their associated strings are copied from the read-only input
136 136 * file images to the output image and their values and index's updated in the
137 137 * output image.
138 138 */
139 139 static Addr
140 140 update_osym(Ofl_desc *ofl)
141 141 {
142 142 /*
143 143 * There are several places in this function where we wish
144 144 * to insert a symbol index to the combined .SUNW_ldynsym/.dynsym
145 145 * symbol table into one of the two sort sections (.SUNW_dynsymsort
146 146 * or .SUNW_dyntlssort), if that symbol has the right attributes.
147 147 * This macro is used to generate the necessary code from a single
148 148 * specification.
149 149 *
150 150 * entry:
151 151 * _sdp, _sym, _type - As per DYNSORT_COUNT. See _libld.h
152 152 * _sym_ndx - Index that _sym will have in the combined
153 153 * .SUNW_ldynsym/.dynsym symbol table.
154 154 */
155 155 #define ADD_TO_DYNSORT(_sdp, _sym, _type, _sym_ndx) \
156 156 { \
157 157 Word *_dynsort_arr, *_dynsort_ndx; \
158 158 \
159 159 if (dynsymsort_symtype[_type]) { \
160 160 _dynsort_arr = dynsymsort; \
161 161 _dynsort_ndx = &dynsymsort_ndx; \
162 162 } else if (_type == STT_TLS) { \
163 163 _dynsort_arr = dyntlssort; \
164 164 _dynsort_ndx = &dyntlssort_ndx; \
165 165 } else { \
166 166 _dynsort_arr = NULL; \
167 167 } \
168 168 if ((_dynsort_arr != NULL) && DYNSORT_TEST_ATTR(_sdp, _sym)) \
169 169 _dynsort_arr[(*_dynsort_ndx)++] = _sym_ndx; \
170 170 }
171 171
172 172 Sym_desc *sdp;
173 173 Sym_avlnode *sav;
174 174 Sg_desc *sgp, *tsgp = NULL, *dsgp = NULL, *esgp = NULL;
175 175 Os_desc *osp, *iosp = NULL, *fosp = NULL;
176 176 Is_desc *isc;
177 177 Ifl_desc *ifl;
178 178 Word bssndx, etext_ndx, edata_ndx = 0, end_ndx, start_ndx;
179 179 Word end_abs = 0, etext_abs = 0, edata_abs;
180 180 Word tlsbssndx = 0, parexpnndx;
181 181 #if defined(_ELF64)
182 182 Word lbssndx = 0;
183 183 Addr lbssaddr = 0;
184 184 #endif
185 185 Addr bssaddr, etext = 0, edata = 0, end = 0, start = 0;
186 186 Addr tlsbssaddr = 0;
187 187 Addr parexpnbase, parexpnaddr;
188 188 int start_set = 0;
189 189 Sym _sym = {0}, *sym, *symtab = NULL;
190 190 Sym *dynsym = NULL, *ldynsym = NULL;
191 191 Word symtab_ndx = 0; /* index into .symtab */
192 192 Word symtab_gbl_bndx; /* .symtab ndx 1st global */
193 193 Word ldynsym_ndx = 0; /* index into .SUNW_ldynsym */
194 194 Word dynsym_ndx = 0; /* index into .dynsym */
195 195 Word scopesym_ndx = 0; /* index into scoped symbols */
196 196 Word scopesym_bndx = 0; /* .symtab ndx 1st scoped sym */
197 197 Word ldynscopesym_ndx = 0; /* index to ldynsym scoped */
198 198 /* symbols */
199 199 Word *dynsymsort = NULL; /* SUNW_dynsymsort index */
200 200 /* vector */
201 201 Word *dyntlssort = NULL; /* SUNW_dyntlssort index */
202 202 /* vector */
203 203 Word dynsymsort_ndx; /* index dynsymsort array */
204 204 Word dyntlssort_ndx; /* index dyntlssort array */
205 205 Word *symndx; /* symbol index (for */
206 206 /* relocation use) */
207 207 Word *symshndx = NULL; /* .symtab_shndx table */
208 208 Word *dynshndx = NULL; /* .dynsym_shndx table */
209 209 Word *ldynshndx = NULL; /* .SUNW_ldynsym_shndx table */
210 210 Word ldynsym_cnt = NULL; /* number of items in */
211 211 /* .SUNW_ldynsym */
212 212 Str_tbl *shstrtab;
213 213 Str_tbl *strtab;
214 214 Str_tbl *dynstr;
215 215 Word *hashtab; /* hash table pointer */
216 216 Word *hashbkt; /* hash table bucket pointer */
217 217 Word *hashchain; /* hash table chain pointer */
218 218 Wk_desc *wkp;
219 219 Alist *weak = NULL;
220 220 ofl_flag_t flags = ofl->ofl_flags;
221 221 Versym *versym;
222 222 Gottable *gottable; /* used for display got debugging */
223 223 /* information */
224 224 Syminfo *syminfo;
225 225 Sym_s_list *sorted_syms; /* table to hold sorted symbols */
226 226 Word ssndx; /* global index into sorted_syms */
227 227 Word scndx; /* scoped index into sorted_syms */
228 228 size_t stoff; /* string offset */
229 229 Aliste idx1;
230 230
231 231 /*
232 232 * Initialize pointers to the symbol table entries and the symbol
233 233 * table strings. Skip the first symbol entry and the first string
234 234 * table byte. Note that if we are not generating any output symbol
235 235 * tables we must still generate and update internal copies so
236 236 * that the relocation phase has the correct information.
237 237 */
238 238 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
239 239 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
240 240 symtab = (Sym *)ofl->ofl_ossymtab->os_outdata->d_buf;
241 241 symtab[symtab_ndx++] = _sym;
242 242 if (ofl->ofl_ossymshndx)
243 243 symshndx =
244 244 (Word *)ofl->ofl_ossymshndx->os_outdata->d_buf;
245 245 }
246 246 if (OFL_ALLOW_DYNSYM(ofl)) {
247 247 dynsym = (Sym *)ofl->ofl_osdynsym->os_outdata->d_buf;
248 248 dynsym[dynsym_ndx++] = _sym;
249 249 /*
250 250 * If we are also constructing a .SUNW_ldynsym section
251 251 * to contain local function symbols, then set it up too.
252 252 */
253 253 if (ofl->ofl_osldynsym) {
254 254 ldynsym = (Sym *)ofl->ofl_osldynsym->os_outdata->d_buf;
255 255 ldynsym[ldynsym_ndx++] = _sym;
256 256 ldynsym_cnt = 1 + ofl->ofl_dynlocscnt +
257 257 ofl->ofl_dynscopecnt;
258 258
259 259 /*
260 260 * If there is a SUNW_ldynsym, then there may also
261 261 * be a .SUNW_dynsymsort and/or .SUNW_dyntlssort
262 262 * sections, used to collect indices of function
263 263 * and data symbols sorted by address order.
264 264 */
265 265 if (ofl->ofl_osdynsymsort) { /* .SUNW_dynsymsort */
266 266 dynsymsort = (Word *)
267 267 ofl->ofl_osdynsymsort->os_outdata->d_buf;
268 268 dynsymsort_ndx = 0;
269 269 }
270 270 if (ofl->ofl_osdyntlssort) { /* .SUNW_dyntlssort */
271 271 dyntlssort = (Word *)
272 272 ofl->ofl_osdyntlssort->os_outdata->d_buf;
273 273 dyntlssort_ndx = 0;
274 274 }
275 275 }
276 276
277 277 /*
278 278 * Initialize the hash table.
279 279 */
280 280 hashtab = (Word *)(ofl->ofl_oshash->os_outdata->d_buf);
281 281 hashbkt = &hashtab[2];
282 282 hashchain = &hashtab[2 + ofl->ofl_hashbkts];
283 283 hashtab[0] = ofl->ofl_hashbkts;
284 284 hashtab[1] = DYNSYM_ALL_CNT(ofl);
285 285 if (ofl->ofl_osdynshndx)
286 286 dynshndx =
287 287 (Word *)ofl->ofl_osdynshndx->os_outdata->d_buf;
288 288 if (ofl->ofl_osldynshndx)
289 289 ldynshndx =
290 290 (Word *)ofl->ofl_osldynshndx->os_outdata->d_buf;
291 291 }
292 292
293 293 /*
294 294 * symndx is the symbol index to be used for relocation processing. It
295 295 * points to the relevant symtab's (.dynsym or .symtab) symbol ndx.
296 296 */
297 297 if (dynsym)
298 298 symndx = &dynsym_ndx;
299 299 else
300 300 symndx = &symtab_ndx;
301 301
302 302 /*
303 303 * If we have version definitions initialize the version symbol index
304 304 * table. There is one entry for each symbol which contains the symbols
305 305 * version index.
306 306 */
307 307 if (!(flags & FLG_OF_NOVERSEC) &&
308 308 (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))) {
309 309 versym = (Versym *)ofl->ofl_osversym->os_outdata->d_buf;
310 310 versym[0] = NULL;
311 311 } else
312 312 versym = NULL;
313 313
314 314 /*
315 315 * If syminfo section exists be prepared to fill it in.
316 316 */
317 317 if (ofl->ofl_ossyminfo) {
318 318 syminfo = ofl->ofl_ossyminfo->os_outdata->d_buf;
319 319 syminfo[0].si_flags = SYMINFO_CURRENT;
320 320 } else
321 321 syminfo = NULL;
322 322
323 323 /*
324 324 * Setup our string tables.
325 325 */
326 326 shstrtab = ofl->ofl_shdrsttab;
327 327 strtab = ofl->ofl_strtab;
328 328 dynstr = ofl->ofl_dynstrtab;
329 329
330 330 DBG_CALL(Dbg_syms_sec_title(ofl->ofl_lml));
331 331
332 332 /*
333 333 * Put output file name to the first .symtab and .SUNW_ldynsym symbol.
334 334 */
335 335 if (symtab) {
336 336 (void) st_setstring(strtab, ofl->ofl_name, &stoff);
337 337 sym = &symtab[symtab_ndx++];
338 338 /* LINTED */
339 339 sym->st_name = stoff;
340 340 sym->st_value = 0;
341 341 sym->st_size = 0;
342 342 sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
343 343 sym->st_other = 0;
344 344 sym->st_shndx = SHN_ABS;
345 345
346 346 if (versym && !dynsym)
347 347 versym[1] = 0;
348 348 }
349 349 if (ldynsym) {
350 350 (void) st_setstring(dynstr, ofl->ofl_name, &stoff);
351 351 sym = &ldynsym[ldynsym_ndx];
352 352 /* LINTED */
353 353 sym->st_name = stoff;
354 354 sym->st_value = 0;
355 355 sym->st_size = 0;
356 356 sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
357 357 sym->st_other = 0;
358 358 sym->st_shndx = SHN_ABS;
359 359
360 360 /* Scoped symbols get filled in global loop below */
361 361 ldynscopesym_ndx = ldynsym_ndx + 1;
362 362 ldynsym_ndx += ofl->ofl_dynscopecnt;
363 363 }
364 364
365 365 /*
366 366 * If we are to display GOT summary information, then allocate
367 367 * the buffer to 'cache' the GOT symbols into now.
368 368 */
369 369 if (DBG_ENABLED) {
370 370 if ((ofl->ofl_gottable = gottable =
371 371 libld_calloc(ofl->ofl_gotcnt, sizeof (Gottable))) == NULL)
372 372 return ((Addr)S_ERROR);
373 373 }
374 374
375 375 /*
376 376 * Traverse the program headers. Determine the last executable segment
377 377 * and the last data segment so that we can update etext and edata. If
378 378 * we have empty segments (reservations) record them for setting _end.
379 379 */
380 380 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
381 381 Phdr *phd = &(sgp->sg_phdr);
382 382 Os_desc *osp;
383 383 Aliste idx2;
384 384
385 385 if (phd->p_type == PT_LOAD) {
386 386 if (sgp->sg_osdescs != NULL) {
387 387 Word _flags = phd->p_flags & (PF_W | PF_R);
388 388
389 389 if (_flags == PF_R)
390 390 tsgp = sgp;
391 391 else if (_flags == (PF_W | PF_R))
392 392 dsgp = sgp;
393 393 } else if (sgp->sg_flags & FLG_SG_EMPTY)
394 394 esgp = sgp;
395 395 }
396 396
397 397 /*
398 398 * Generate a section symbol for each output section.
399 399 */
400 400 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
401 401 Word sectndx;
402 402
403 403 sym = &_sym;
404 404 sym->st_value = osp->os_shdr->sh_addr;
405 405 sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
406 406 /* LINTED */
407 407 sectndx = elf_ndxscn(osp->os_scn);
408 408
409 409 if (symtab) {
410 410 if (sectndx >= SHN_LORESERVE) {
411 411 symshndx[symtab_ndx] = sectndx;
412 412 sym->st_shndx = SHN_XINDEX;
413 413 } else {
414 414 /* LINTED */
415 415 sym->st_shndx = (Half)sectndx;
416 416 }
417 417 symtab[symtab_ndx++] = *sym;
418 418 }
419 419
420 420 if (dynsym && (osp->os_flags & FLG_OS_OUTREL))
421 421 dynsym[dynsym_ndx++] = *sym;
422 422
423 423 if ((dynsym == NULL) ||
424 424 (osp->os_flags & FLG_OS_OUTREL)) {
425 425 if (versym)
426 426 versym[*symndx - 1] = 0;
427 427 osp->os_identndx = *symndx - 1;
428 428 DBG_CALL(Dbg_syms_sec_entry(ofl->ofl_lml,
429 429 osp->os_identndx, sgp, osp));
430 430 }
431 431
432 432 /*
433 433 * Generate the .shstrtab for this section.
434 434 */
435 435 (void) st_setstring(shstrtab, osp->os_name, &stoff);
436 436 osp->os_shdr->sh_name = (Word)stoff;
437 437
438 438 /*
439 439 * Find the section index for our special symbols.
440 440 */
441 441 if (sgp == tsgp) {
442 442 /* LINTED */
443 443 etext_ndx = elf_ndxscn(osp->os_scn);
444 444 } else if (dsgp == sgp) {
445 445 if (osp->os_shdr->sh_type != SHT_NOBITS) {
446 446 /* LINTED */
447 447 edata_ndx = elf_ndxscn(osp->os_scn);
448 448 }
449 449 }
450 450
451 451 if (start_set == 0) {
452 452 start = sgp->sg_phdr.p_vaddr;
453 453 /* LINTED */
454 454 start_ndx = elf_ndxscn(osp->os_scn);
455 455 start_set++;
456 456 }
457 457
458 458 /*
459 459 * While we're here, determine whether a .init or .fini
460 460 * section exist.
461 461 */
462 462 if ((iosp == NULL) && (strcmp(osp->os_name,
463 463 MSG_ORIG(MSG_SCN_INIT)) == 0))
464 464 iosp = osp;
465 465 if ((fosp == NULL) && (strcmp(osp->os_name,
466 466 MSG_ORIG(MSG_SCN_FINI)) == 0))
467 467 fosp = osp;
468 468 }
469 469 }
470 470
471 471 /*
472 472 * Add local register symbols to the .dynsym. These are required as
473 473 * DT_REGISTER .dynamic entries must have a symbol to reference.
474 474 */
475 475 if (ofl->ofl_regsyms && dynsym) {
476 476 int ndx;
477 477
478 478 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
479 479 Sym_desc *rsdp;
480 480
481 481 if ((rsdp = ofl->ofl_regsyms[ndx]) == NULL)
482 482 continue;
483 483
484 484 if (!SYM_IS_HIDDEN(rsdp) &&
485 485 (ELF_ST_BIND(rsdp->sd_sym->st_info) != STB_LOCAL))
486 486 continue;
487 487
488 488 dynsym[dynsym_ndx] = *(rsdp->sd_sym);
489 489 rsdp->sd_symndx = *symndx;
490 490
491 491 if (dynsym[dynsym_ndx].st_name) {
492 492 (void) st_setstring(dynstr, rsdp->sd_name,
493 493 &stoff);
494 494 dynsym[dynsym_ndx].st_name = stoff;
495 495 }
496 496 dynsym_ndx++;
497 497 }
498 498 }
499 499
500 500 /*
501 501 * Having traversed all the output segments, warn the user if the
502 502 * traditional text or data segments don't exist. Otherwise from these
503 503 * segments establish the values for `etext', `edata', `end', `END',
504 504 * and `START'.
505 505 */
506 506 if (!(flags & FLG_OF_RELOBJ)) {
507 507 Sg_desc *sgp;
508 508
509 509 if (tsgp)
510 510 etext = tsgp->sg_phdr.p_vaddr + tsgp->sg_phdr.p_filesz;
511 511 else {
512 512 etext = (Addr)0;
513 513 etext_ndx = SHN_ABS;
514 514 etext_abs = 1;
515 515 if (flags & FLG_OF_VERBOSE)
516 516 ld_eprintf(ofl, ERR_WARNING,
517 517 MSG_INTL(MSG_UPD_NOREADSEG));
518 518 }
519 519 if (dsgp) {
520 520 edata = dsgp->sg_phdr.p_vaddr + dsgp->sg_phdr.p_filesz;
521 521 } else {
522 522 edata = (Addr)0;
523 523 edata_ndx = SHN_ABS;
524 524 edata_abs = 1;
525 525 if (flags & FLG_OF_VERBOSE)
526 526 ld_eprintf(ofl, ERR_WARNING,
527 527 MSG_INTL(MSG_UPD_NORDWRSEG));
528 528 }
529 529
530 530 if (dsgp == NULL) {
531 531 if (tsgp)
532 532 sgp = tsgp;
533 533 else
534 534 sgp = 0;
535 535 } else if (tsgp == NULL)
536 536 sgp = dsgp;
537 537 else if (dsgp->sg_phdr.p_vaddr > tsgp->sg_phdr.p_vaddr)
538 538 sgp = dsgp;
539 539 else if (dsgp->sg_phdr.p_vaddr < tsgp->sg_phdr.p_vaddr)
540 540 sgp = tsgp;
541 541 else {
542 542 /*
543 543 * One of the segments must be of zero size.
544 544 */
545 545 if (tsgp->sg_phdr.p_memsz)
546 546 sgp = tsgp;
547 547 else
548 548 sgp = dsgp;
549 549 }
550 550
551 551 if (esgp && (esgp->sg_phdr.p_vaddr > sgp->sg_phdr.p_vaddr))
552 552 sgp = esgp;
553 553
554 554 if (sgp) {
555 555 end = sgp->sg_phdr.p_vaddr + sgp->sg_phdr.p_memsz;
556 556
557 557 /*
558 558 * If the last loadable segment is a read-only segment,
559 559 * then the application which uses the symbol _end to
560 560 * find the beginning of writable heap area may cause
561 561 * segmentation violation. We adjust the value of the
562 562 * _end to skip to the next page boundary.
563 563 *
564 564 * 6401812 System interface which returs beginning
565 565 * heap would be nice.
566 566 * When the above RFE is implemented, the changes below
567 567 * could be changed in a better way.
568 568 */
569 569 if ((sgp->sg_phdr.p_flags & PF_W) == 0)
570 570 end = (Addr)S_ROUND(end, sysconf(_SC_PAGESIZE));
571 571
572 572 /*
573 573 * If we're dealing with a memory reservation there are
574 574 * no sections to establish an index for _end, so assign
575 575 * it as an absolute.
576 576 */
577 577 if (sgp->sg_osdescs != NULL) {
578 578 /*
579 579 * Determine the last section for this segment.
580 580 */
581 581 Os_desc *osp = sgp->sg_osdescs->apl_data
582 582 [sgp->sg_osdescs->apl_nitems - 1];
583 583
584 584 /* LINTED */
585 585 end_ndx = elf_ndxscn(osp->os_scn);
586 586 } else {
587 587 end_ndx = SHN_ABS;
588 588 end_abs = 1;
589 589 }
590 590 } else {
591 591 end = (Addr) 0;
592 592 end_ndx = SHN_ABS;
593 593 end_abs = 1;
594 594 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_UPD_NOSEG));
595 595 }
596 596 }
597 597
598 598 /*
599 599 * Initialize the scoped symbol table entry point. This is for all
600 600 * the global symbols that have been scoped to locals and will be
601 601 * filled in during global symbol processing so that we don't have
602 602 * to traverse the globals symbol hash array more than once.
603 603 */
604 604 if (symtab) {
605 605 scopesym_bndx = symtab_ndx;
606 606 scopesym_ndx = scopesym_bndx;
607 607 symtab_ndx += ofl->ofl_scopecnt;
608 608 }
609 609
610 610 /*
611 611 * If expanding partially expanded symbols under '-z nopartial',
612 612 * prepare to do that.
613 613 */
614 614 if (ofl->ofl_isparexpn) {
615 615 osp = ofl->ofl_isparexpn->is_osdesc;
616 616 parexpnbase = parexpnaddr = (Addr)(osp->os_shdr->sh_addr +
617 617 ofl->ofl_isparexpn->is_indata->d_off);
618 618 /* LINTED */
619 619 parexpnndx = elf_ndxscn(osp->os_scn);
620 620 ofl->ofl_parexpnndx = osp->os_identndx;
621 621 }
622 622
623 623 /*
624 624 * If we are generating a .symtab collect all the local symbols,
625 625 * assigning a new virtual address or displacement (value).
626 626 */
627 627 for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
628 628 Xword lndx, local = ifl->ifl_locscnt;
629 629 Cap_desc *cdp = ifl->ifl_caps;
630 630
631 631 for (lndx = 1; lndx < local; lndx++) {
632 632 Gotndx *gnp;
633 633 uchar_t type;
634 634 Word *_symshndx;
635 635 int enter_in_symtab, enter_in_ldynsym;
636 636 int update_done;
637 637
638 638 sdp = ifl->ifl_oldndx[lndx];
639 639 sym = sdp->sd_sym;
640 640
641 641 /*
642 642 * Assign a got offset if necessary.
643 643 */
644 644 if ((ld_targ.t_mr.mr_assign_got != NULL) &&
645 645 (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
646 646 return ((Addr)S_ERROR);
647 647
648 648 if (DBG_ENABLED) {
649 649 Aliste idx2;
650 650
651 651 for (ALIST_TRAVERSE(sdp->sd_GOTndxs,
652 652 idx2, gnp)) {
653 653 gottable->gt_sym = sdp;
654 654 gottable->gt_gndx.gn_gotndx =
655 655 gnp->gn_gotndx;
656 656 gottable->gt_gndx.gn_addend =
657 657 gnp->gn_addend;
658 658 gottable++;
659 659 }
660 660 }
661 661
662 662 if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION)
663 663 continue;
664 664
665 665 /*
666 666 * Ignore any symbols that have been marked as invalid
667 667 * during input processing. Providing these aren't used
668 668 * for relocation they'll just be dropped from the
669 669 * output image.
670 670 */
671 671 if (sdp->sd_flags & FLG_SY_INVALID)
672 672 continue;
673 673
674 674 /*
675 675 * If the section that this symbol was associated
676 676 * with has been discarded - then we discard
677 677 * the local symbol along with it.
678 678 */
679 679 if (sdp->sd_flags & FLG_SY_ISDISC)
680 680 continue;
681 681
682 682 /*
683 683 * If this symbol is from a different file
684 684 * than the input descriptor we are processing,
685 685 * treat it as if it has FLG_SY_ISDISC set.
686 686 * This happens when sloppy_comdat_reloc()
687 687 * replaces a symbol to a discarded comdat section
688 688 * with an equivalent symbol from a different
689 689 * file. We only want to enter such a symbol
690 690 * once --- as part of the file that actually
691 691 * supplies it.
692 692 */
693 693 if (ifl != sdp->sd_file)
694 694 continue;
695 695
696 696 /*
697 697 * Generate an output symbol to represent this input
698 698 * symbol. Even if the symbol table is to be stripped
699 699 * we still need to update any local symbols that are
700 700 * used during relocation.
701 701 */
702 702 enter_in_symtab = symtab &&
703 703 (!(ofl->ofl_flags & FLG_OF_REDLSYM) ||
704 704 sdp->sd_move);
705 705 enter_in_ldynsym = ldynsym && sdp->sd_name &&
706 706 ldynsym_symtype[type] &&
707 707 !(ofl->ofl_flags & FLG_OF_REDLSYM);
708 708 _symshndx = NULL;
709 709
710 710 if (enter_in_symtab) {
711 711 if (!dynsym)
712 712 sdp->sd_symndx = *symndx;
713 713 symtab[symtab_ndx] = *sym;
714 714
715 715 /*
716 716 * Provided this isn't an unnamed register
717 717 * symbol, update its name.
718 718 */
719 719 if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
720 720 symtab[symtab_ndx].st_name) {
721 721 (void) st_setstring(strtab,
722 722 sdp->sd_name, &stoff);
723 723 symtab[symtab_ndx].st_name = stoff;
724 724 }
725 725 sdp->sd_flags &= ~FLG_SY_CLEAN;
726 726 if (symshndx)
727 727 _symshndx = &symshndx[symtab_ndx];
728 728 sdp->sd_sym = sym = &symtab[symtab_ndx++];
729 729
730 730 if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
731 731 (sym->st_shndx == SHN_ABS) &&
732 732 !enter_in_ldynsym)
733 733 continue;
734 734 } else if (enter_in_ldynsym) {
735 735 /*
736 736 * Not using symtab, but we do have ldynsym
737 737 * available.
738 738 */
739 739 ldynsym[ldynsym_ndx] = *sym;
740 740 (void) st_setstring(dynstr, sdp->sd_name,
741 741 &stoff);
742 742 ldynsym[ldynsym_ndx].st_name = stoff;
743 743
744 744 sdp->sd_flags &= ~FLG_SY_CLEAN;
745 745 if (ldynshndx)
746 746 _symshndx = &ldynshndx[ldynsym_ndx];
747 747 sdp->sd_sym = sym = &ldynsym[ldynsym_ndx];
748 748 /* Add it to sort section if it qualifies */
749 749 ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
750 750 ldynsym_ndx++;
751 751 } else { /* Not using symtab or ldynsym */
752 752 /*
753 753 * If this symbol requires modifying to provide
754 754 * for a relocation or move table update, make
755 755 * a copy of it.
756 756 */
757 757 if (!(sdp->sd_flags & FLG_SY_UPREQD) &&
758 758 !(sdp->sd_move))
759 759 continue;
760 760 if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
761 761 (sym->st_shndx == SHN_ABS))
762 762 continue;
763 763
764 764 if (ld_sym_copy(sdp) == S_ERROR)
765 765 return ((Addr)S_ERROR);
766 766 sym = sdp->sd_sym;
767 767 }
768 768
769 769 /*
770 770 * Update the symbols contents if necessary.
771 771 */
772 772 update_done = 0;
773 773 if (type == STT_FILE) {
774 774 sdp->sd_shndx = sym->st_shndx = SHN_ABS;
775 775 sdp->sd_flags |= FLG_SY_SPECSEC;
776 776 update_done = 1;
777 777 }
778 778
779 779 /*
780 780 * If we are expanding the locally bound partially
781 781 * initialized symbols, then update the address here.
782 782 */
783 783 if (ofl->ofl_isparexpn &&
784 784 (sdp->sd_flags & FLG_SY_PAREXPN) && !update_done) {
785 785 sym->st_shndx = parexpnndx;
786 786 sdp->sd_isc = ofl->ofl_isparexpn;
787 787 sym->st_value = parexpnaddr;
788 788 parexpnaddr += sym->st_size;
789 789 if ((flags & FLG_OF_RELOBJ) == 0)
790 790 sym->st_value -= parexpnbase;
791 791 }
792 792
793 793 /*
794 794 * If this isn't an UNDEF symbol (ie. an input section
795 795 * is associated), update the symbols value and index.
796 796 */
797 797 if (((isc = sdp->sd_isc) != NULL) && !update_done) {
798 798 Word sectndx;
799 799
800 800 osp = isc->is_osdesc;
801 801 /* LINTED */
802 802 sym->st_value +=
803 803 (Off)_elf_getxoff(isc->is_indata);
804 804 if ((flags & FLG_OF_RELOBJ) == 0) {
805 805 sym->st_value += osp->os_shdr->sh_addr;
806 806 /*
807 807 * TLS symbols are relative to
808 808 * the TLS segment.
809 809 */
810 810 if ((type == STT_TLS) &&
811 811 (ofl->ofl_tlsphdr)) {
812 812 sym->st_value -=
813 813 ofl->ofl_tlsphdr->p_vaddr;
814 814 }
815 815 }
816 816 /* LINTED */
817 817 if ((sdp->sd_shndx = sectndx =
818 818 elf_ndxscn(osp->os_scn)) >= SHN_LORESERVE) {
819 819 if (_symshndx) {
820 820 *_symshndx = sectndx;
821 821 }
822 822 sym->st_shndx = SHN_XINDEX;
823 823 } else {
824 824 /* LINTED */
825 825 sym->st_shndx = sectndx;
826 826 }
827 827 }
828 828
829 829 /*
830 830 * If entering the symbol in both the symtab and the
831 831 * ldynsym, then the one in symtab needs to be
832 832 * copied to ldynsym. If it is only in the ldynsym,
833 833 * then the code above already set it up and we have
834 834 * nothing more to do here.
835 835 */
836 836 if (enter_in_symtab && enter_in_ldynsym) {
837 837 ldynsym[ldynsym_ndx] = *sym;
838 838 (void) st_setstring(dynstr, sdp->sd_name,
839 839 &stoff);
840 840 ldynsym[ldynsym_ndx].st_name = stoff;
841 841
842 842 if (_symshndx && ldynshndx)
843 843 ldynshndx[ldynsym_ndx] = *_symshndx;
844 844
845 845 /* Add it to sort section if it qualifies */
846 846 ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
847 847
848 848 ldynsym_ndx++;
849 849 }
850 850 }
851 851
852 852 /*
853 853 * If this input file has undergone object to symbol
854 854 * capabilities conversion, supply any new capabilities symbols.
855 855 * These symbols are copies of the original global symbols, and
856 856 * follow the existing local symbols that are supplied from this
857 857 * input file (which are identified with a preceding STT_FILE).
858 858 */
859 859 if (symtab && cdp && cdp->ca_syms) {
860 860 Aliste idx2;
861 861 Cap_sym *csp;
862 862
863 863 for (APLIST_TRAVERSE(cdp->ca_syms, idx2, csp)) {
864 864 Is_desc *isp;
865 865
866 866 sdp = csp->cs_sdp;
867 867 sym = sdp->sd_sym;
868 868
869 869 if ((isp = sdp->sd_isc) != NULL) {
870 870 Os_desc *osp = isp->is_osdesc;
871 871
872 872 /*
873 873 * Update the symbols value.
874 874 */
875 875 /* LINTED */
876 876 sym->st_value +=
877 877 (Off)_elf_getxoff(isp->is_indata);
878 878 if ((flags & FLG_OF_RELOBJ) == 0)
879 879 sym->st_value +=
880 880 osp->os_shdr->sh_addr;
881 881
882 882 /*
883 883 * Update the symbols section index.
884 884 */
885 885 sdp->sd_shndx = sym->st_shndx =
886 886 elf_ndxscn(osp->os_scn);
887 887 }
888 888
889 889 symtab[symtab_ndx] = *sym;
890 890 (void) st_setstring(strtab, sdp->sd_name,
891 891 &stoff);
892 892 symtab[symtab_ndx].st_name = stoff;
893 893 sdp->sd_symndx = symtab_ndx++;
894 894 }
895 895 }
896 896 }
897 897
898 898 symtab_gbl_bndx = symtab_ndx; /* .symtab index of 1st global entry */
899 899
900 900 /*
901 901 * Two special symbols are `_init' and `_fini'. If these are supplied
902 902 * by crti.o then they are used to represent the total concatenation of
903 903 * the `.init' and `.fini' sections.
904 904 *
905 905 * Determine whether any .init or .fini sections exist. If these
906 906 * sections exist and a dynamic object is being built, but no `_init'
907 907 * or `_fini' symbols are found, then the user is probably building
908 908 * this object directly from ld(1) rather than using a compiler driver
909 909 * that provides the symbols via crt's.
910 910 *
911 911 * If the .init or .fini section exist, and their associated symbols,
912 912 * determine the size of the sections and updated the symbols value
913 913 * accordingly.
914 914 */
915 915 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), SYM_NOHASH, 0,
916 916 ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
917 917 (sdp->sd_isc->is_osdesc == iosp)) {
918 918 if (ld_sym_copy(sdp) == S_ERROR)
919 919 return ((Addr)S_ERROR);
920 920 sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;
921 921
922 922 } else if (iosp && !(flags & FLG_OF_RELOBJ)) {
923 923 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
924 924 MSG_ORIG(MSG_SYM_INIT_U), MSG_ORIG(MSG_SCN_INIT));
925 925 }
926 926
927 927 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), SYM_NOHASH, 0,
928 928 ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
929 929 (sdp->sd_isc->is_osdesc == fosp)) {
930 930 if (ld_sym_copy(sdp) == S_ERROR)
931 931 return ((Addr)S_ERROR);
932 932 sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;
933 933
934 934 } else if (fosp && !(flags & FLG_OF_RELOBJ)) {
935 935 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
936 936 MSG_ORIG(MSG_SYM_FINI_U), MSG_ORIG(MSG_SCN_FINI));
937 937 }
938 938
939 939 /*
940 940 * Assign .bss information for use with updating COMMON symbols.
941 941 */
942 942 if (ofl->ofl_isbss) {
943 943 isc = ofl->ofl_isbss;
944 944 osp = isc->is_osdesc;
945 945
946 946 bssaddr = osp->os_shdr->sh_addr +
947 947 (Off)_elf_getxoff(isc->is_indata);
948 948 /* LINTED */
949 949 bssndx = elf_ndxscn(osp->os_scn);
950 950 }
951 951
952 952 #if defined(_ELF64)
953 953 /*
954 954 * For amd64 target, assign .lbss information for use
955 955 * with updating LCOMMON symbols.
956 956 */
957 957 if ((ld_targ.t_m.m_mach == EM_AMD64) && ofl->ofl_islbss) {
958 958 osp = ofl->ofl_islbss->is_osdesc;
959 959
960 960 lbssaddr = osp->os_shdr->sh_addr +
961 961 (Off)_elf_getxoff(ofl->ofl_islbss->is_indata);
962 962 /* LINTED */
963 963 lbssndx = elf_ndxscn(osp->os_scn);
964 964 }
965 965 #endif
966 966 /*
967 967 * Assign .tlsbss information for use with updating COMMON symbols.
968 968 */
969 969 if (ofl->ofl_istlsbss) {
970 970 osp = ofl->ofl_istlsbss->is_osdesc;
971 971 tlsbssaddr = osp->os_shdr->sh_addr +
972 972 (Off)_elf_getxoff(ofl->ofl_istlsbss->is_indata);
973 973 /* LINTED */
974 974 tlsbssndx = elf_ndxscn(osp->os_scn);
975 975 }
976 976
977 977 if ((sorted_syms = libld_calloc(ofl->ofl_globcnt +
978 978 ofl->ofl_elimcnt + ofl->ofl_scopecnt,
979 979 sizeof (*sorted_syms))) == NULL)
980 980 return ((Addr)S_ERROR);
981 981
982 982 scndx = 0;
983 983 ssndx = ofl->ofl_scopecnt + ofl->ofl_elimcnt;
984 984
985 985 DBG_CALL(Dbg_syms_up_title(ofl->ofl_lml));
986 986
987 987 /*
988 988 * Traverse the internal symbol table updating global symbol information
989 989 * and allocating common.
990 990 */
991 991 for (sav = avl_first(&ofl->ofl_symavl); sav;
992 992 sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
993 993 Sym *symptr;
994 994 int local;
995 995 int restore;
996 996
997 997 sdp = sav->sav_sdp;
998 998
999 999 /*
1000 1000 * Ignore any symbols that have been marked as invalid during
1001 1001 * input processing. Providing these aren't used for
1002 1002 * relocation, they will be dropped from the output image.
1003 1003 */
1004 1004 if (sdp->sd_flags & FLG_SY_INVALID) {
1005 1005 DBG_CALL(Dbg_syms_old(ofl, sdp));
1006 1006 DBG_CALL(Dbg_syms_ignore(ofl, sdp));
1007 1007 continue;
1008 1008 }
1009 1009
1010 1010 /*
1011 1011 * Only needed symbols are copied to the output symbol table.
1012 1012 */
1013 1013 if (sdp->sd_ref == REF_DYN_SEEN)
1014 1014 continue;
1015 1015
1016 1016 if (SYM_IS_HIDDEN(sdp) && (flags & FLG_OF_PROCRED))
1017 1017 local = 1;
1018 1018 else
1019 1019 local = 0;
1020 1020
1021 1021 if (local || (ofl->ofl_hashbkts == 0)) {
1022 1022 sorted_syms[scndx++].sl_sdp = sdp;
1023 1023 } else {
1024 1024 sorted_syms[ssndx].sl_hval = sdp->sd_aux->sa_hash %
1025 1025 ofl->ofl_hashbkts;
1026 1026 sorted_syms[ssndx].sl_sdp = sdp;
1027 1027 ssndx++;
1028 1028 }
1029 1029
1030 1030 /*
1031 1031 * Note - expand the COMMON symbols here because an address
1032 1032 * must be assigned to them in the same order that space was
1033 1033 * calculated in sym_validate(). If this ordering isn't
1034 1034 * followed differing alignment requirements can throw us all
1035 1035 * out of whack.
1036 1036 *
1037 1037 * The expanded .bss global symbol is handled here as well.
1038 1038 *
1039 1039 * The actual adding entries into the symbol table still occurs
1040 1040 * below in hashbucket order.
1041 1041 */
1042 1042 symptr = sdp->sd_sym;
1043 1043 restore = 0;
1044 1044 if ((sdp->sd_flags & FLG_SY_PAREXPN) ||
1045 1045 ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1046 1046 (sdp->sd_shndx = symptr->st_shndx) == SHN_COMMON)) {
1047 1047
1048 1048 /*
1049 1049 * An expanded symbol goes to a special .data section
1050 1050 * prepared for that purpose (ofl->ofl_isparexpn).
1051 1051 * Assign COMMON allocations to .bss.
1052 1052 * Otherwise leave it as is.
1053 1053 */
1054 1054 if (sdp->sd_flags & FLG_SY_PAREXPN) {
1055 1055 restore = 1;
1056 1056 sdp->sd_shndx = parexpnndx;
1057 1057 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1058 1058 symptr->st_value = (Xword) S_ROUND(
1059 1059 parexpnaddr, symptr->st_value);
1060 1060 parexpnaddr = symptr->st_value +
1061 1061 symptr->st_size;
1062 1062 sdp->sd_isc = ofl->ofl_isparexpn;
1063 1063 sdp->sd_flags |= FLG_SY_COMMEXP;
1064 1064
1065 1065 } else if (ELF_ST_TYPE(symptr->st_info) != STT_TLS &&
1066 1066 (local || !(flags & FLG_OF_RELOBJ))) {
1067 1067 restore = 1;
1068 1068 sdp->sd_shndx = bssndx;
1069 1069 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1070 1070 symptr->st_value = (Xword)S_ROUND(bssaddr,
1071 1071 symptr->st_value);
1072 1072 bssaddr = symptr->st_value + symptr->st_size;
1073 1073 sdp->sd_isc = ofl->ofl_isbss;
1074 1074 sdp->sd_flags |= FLG_SY_COMMEXP;
1075 1075
1076 1076 } else if (ELF_ST_TYPE(symptr->st_info) == STT_TLS &&
1077 1077 (local || !(flags & FLG_OF_RELOBJ))) {
1078 1078 restore = 1;
1079 1079 sdp->sd_shndx = tlsbssndx;
1080 1080 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1081 1081 symptr->st_value = (Xword)S_ROUND(tlsbssaddr,
1082 1082 symptr->st_value);
1083 1083 tlsbssaddr = symptr->st_value + symptr->st_size;
1084 1084 sdp->sd_isc = ofl->ofl_istlsbss;
1085 1085 sdp->sd_flags |= FLG_SY_COMMEXP;
1086 1086 /*
1087 1087 * TLS symbols are relative to the TLS segment.
1088 1088 */
1089 1089 symptr->st_value -= ofl->ofl_tlsphdr->p_vaddr;
1090 1090 }
1091 1091 #if defined(_ELF64)
1092 1092 } else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1093 1093 (sdp->sd_flags & FLG_SY_SPECSEC) &&
1094 1094 ((sdp->sd_shndx = symptr->st_shndx) ==
1095 1095 SHN_X86_64_LCOMMON) &&
1096 1096 ((local || !(flags & FLG_OF_RELOBJ)))) {
1097 1097 restore = 1;
1098 1098 sdp->sd_shndx = lbssndx;
1099 1099 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1100 1100 symptr->st_value = (Xword)S_ROUND(lbssaddr,
1101 1101 symptr->st_value);
1102 1102 lbssaddr = symptr->st_value + symptr->st_size;
1103 1103 sdp->sd_isc = ofl->ofl_islbss;
1104 1104 sdp->sd_flags |= FLG_SY_COMMEXP;
1105 1105 #endif
1106 1106 }
1107 1107
1108 1108 if (restore != 0) {
1109 1109 uchar_t type, bind;
1110 1110
1111 1111 /*
1112 1112 * Make sure this COMMON symbol is returned to the same
1113 1113 * binding as was defined in the original relocatable
1114 1114 * object reference.
1115 1115 */
1116 1116 type = ELF_ST_TYPE(symptr->st_info);
1117 1117 if (sdp->sd_flags & FLG_SY_GLOBREF)
1118 1118 bind = STB_GLOBAL;
1119 1119 else
1120 1120 bind = STB_WEAK;
1121 1121
1122 1122 symptr->st_info = ELF_ST_INFO(bind, type);
1123 1123 }
1124 1124 }
1125 1125
1126 1126 /*
1127 1127 * If this is a dynamic object then add any local capabilities symbols.
1128 1128 */
1129 1129 if (dynsym && ofl->ofl_capfamilies) {
1130 1130 Cap_avlnode *cav;
1131 1131
1132 1132 for (cav = avl_first(ofl->ofl_capfamilies); cav;
1133 1133 cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
1134 1134 Cap_sym *csp;
1135 1135 Aliste idx;
1136 1136
1137 1137 for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
1138 1138 sdp = csp->cs_sdp;
1139 1139
1140 1140 DBG_CALL(Dbg_syms_created(ofl->ofl_lml,
1141 1141 sdp->sd_name));
1142 1142 DBG_CALL(Dbg_syms_entered(ofl, sdp->sd_sym,
1143 1143 sdp));
1144 1144
1145 1145 dynsym[dynsym_ndx] = *sdp->sd_sym;
1146 1146
1147 1147 (void) st_setstring(dynstr, sdp->sd_name,
1148 1148 &stoff);
1149 1149 dynsym[dynsym_ndx].st_name = stoff;
1150 1150
1151 1151 sdp->sd_sym = &dynsym[dynsym_ndx];
1152 1152 sdp->sd_symndx = dynsym_ndx;
1153 1153
1154 1154 /*
1155 1155 * Indicate that this is a capabilities symbol.
1156 1156 * Note, that this identification only provides
1157 1157 * information regarding the symbol that is
1158 1158 * visible from elfdump(1) -y. The association
1159 1159 * of a symbol to its capabilities is derived
1160 1160 * from a .SUNW_capinfo entry.
1161 1161 */
1162 1162 if (syminfo) {
1163 1163 syminfo[dynsym_ndx].si_flags |=
1164 1164 SYMINFO_FLG_CAP;
1165 1165 }
1166 1166
1167 1167 dynsym_ndx++;
1168 1168 }
1169 1169 }
1170 1170 }
1171 1171
1172 1172 if (ofl->ofl_hashbkts) {
1173 1173 qsort(sorted_syms + ofl->ofl_scopecnt + ofl->ofl_elimcnt,
1174 1174 ofl->ofl_globcnt, sizeof (Sym_s_list),
1175 1175 (int (*)(const void *, const void *))sym_hash_compare);
1176 1176 }
1177 1177
1178 1178 for (ssndx = 0; ssndx < (ofl->ofl_elimcnt + ofl->ofl_scopecnt +
1179 1179 ofl->ofl_globcnt); ssndx++) {
1180 1180 const char *name;
1181 1181 Sym *sym;
1182 1182 Sym_aux *sap;
1183 1183 Half spec;
1184 1184 int local = 0, dynlocal = 0, enter_in_symtab;
1185 1185 Gotndx *gnp;
1186 1186 Word sectndx;
1187 1187
1188 1188 sdp = sorted_syms[ssndx].sl_sdp;
1189 1189 sectndx = 0;
1190 1190
1191 1191 if (symtab)
1192 1192 enter_in_symtab = 1;
1193 1193 else
1194 1194 enter_in_symtab = 0;
1195 1195
1196 1196 /*
1197 1197 * Assign a got offset if necessary.
1198 1198 */
1199 1199 if ((ld_targ.t_mr.mr_assign_got != NULL) &&
1200 1200 (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
1201 1201 return ((Addr)S_ERROR);
1202 1202
1203 1203 if (DBG_ENABLED) {
1204 1204 Aliste idx2;
1205 1205
1206 1206 for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx2, gnp)) {
1207 1207 gottable->gt_sym = sdp;
1208 1208 gottable->gt_gndx.gn_gotndx = gnp->gn_gotndx;
1209 1209 gottable->gt_gndx.gn_addend = gnp->gn_addend;
1210 1210 gottable++;
1211 1211 }
1212 1212
1213 1213 if (sdp->sd_aux && sdp->sd_aux->sa_PLTGOTndx) {
1214 1214 gottable->gt_sym = sdp;
1215 1215 gottable->gt_gndx.gn_gotndx =
1216 1216 sdp->sd_aux->sa_PLTGOTndx;
1217 1217 gottable++;
1218 1218 }
1219 1219 }
1220 1220
1221 1221 /*
1222 1222 * If this symbol has been marked as being reduced to local
1223 1223 * scope then it will have to be placed in the scoped portion
1224 1224 * of the .symtab. Retain the appropriate index for use in
1225 1225 * version symbol indexing and relocation.
1226 1226 */
1227 1227 if (SYM_IS_HIDDEN(sdp) && (flags & FLG_OF_PROCRED)) {
1228 1228 local = 1;
1229 1229 if (!(sdp->sd_flags & FLG_SY_ELIM) && !dynsym)
1230 1230 sdp->sd_symndx = scopesym_ndx;
1231 1231 else
1232 1232 sdp->sd_symndx = 0;
1233 1233
1234 1234 if (sdp->sd_flags & FLG_SY_ELIM) {
1235 1235 enter_in_symtab = 0;
1236 1236 } else if (ldynsym && sdp->sd_sym->st_name &&
1237 1237 ldynsym_symtype[
1238 1238 ELF_ST_TYPE(sdp->sd_sym->st_info)]) {
1239 1239 dynlocal = 1;
1240 1240 }
1241 1241 } else {
1242 1242 sdp->sd_symndx = *symndx;
1243 1243 }
1244 1244
1245 1245 /*
1246 1246 * Copy basic symbol and string information.
1247 1247 */
1248 1248 name = sdp->sd_name;
1249 1249 sap = sdp->sd_aux;
1250 1250
1251 1251 /*
1252 1252 * If we require to record version symbol indexes, update the
1253 1253 * associated version symbol information for all defined
1254 1254 * symbols. If a version definition is required any zero value
1255 1255 * symbol indexes would have been flagged as undefined symbol
1256 1256 * errors, however if we're just scoping these need to fall into
1257 1257 * the base of global symbols.
1258 1258 */
1259 1259 if (sdp->sd_symndx && versym) {
1260 1260 Half vndx = 0;
1261 1261
1262 1262 if (sdp->sd_flags & FLG_SY_MVTOCOMM) {
1263 1263 vndx = VER_NDX_GLOBAL;
1264 1264 } else if (sdp->sd_ref == REF_REL_NEED) {
1265 1265 vndx = sap->sa_overndx;
1266 1266
1267 1267 if ((vndx == 0) &&
1268 1268 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1269 1269 if (SYM_IS_HIDDEN(sdp))
1270 1270 vndx = VER_NDX_LOCAL;
1271 1271 else
1272 1272 vndx = VER_NDX_GLOBAL;
1273 1273 }
1274 1274 } else if ((sdp->sd_ref == REF_DYN_NEED) &&
1275 1275 (sap->sa_dverndx > 0) &&
1276 1276 (sap->sa_dverndx <= sdp->sd_file->ifl_vercnt) &&
1277 1277 (sdp->sd_file->ifl_verndx != NULL)) {
1278 1278 /* Use index of verneed record */
1279 1279 vndx = sdp->sd_file->ifl_verndx
1280 1280 [sap->sa_dverndx].vi_overndx;
1281 1281 }
1282 1282 versym[sdp->sd_symndx] = vndx;
1283 1283 }
1284 1284
1285 1285 /*
1286 1286 * If we are creating the .syminfo section then set per symbol
1287 1287 * flags here.
1288 1288 */
1289 1289 if (sdp->sd_symndx && syminfo &&
1290 1290 !(sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1291 1291 int ndx = sdp->sd_symndx;
1292 1292 APlist **alpp = &(ofl->ofl_symdtent);
1293 1293
1294 1294 if (sdp->sd_flags & FLG_SY_MVTOCOMM)
1295 1295 /*
1296 1296 * Identify a copy relocation symbol.
1297 1297 */
1298 1298 syminfo[ndx].si_flags |= SYMINFO_FLG_COPY;
1299 1299
1300 1300 if (sdp->sd_ref == REF_DYN_NEED) {
1301 1301 /*
1302 1302 * A reference is bound to a needed dependency.
1303 1303 * Save the syminfo entry, so that when the
1304 1304 * .dynamic section has been updated, a
1305 1305 * DT_NEEDED entry can be associated
1306 1306 * (see update_osyminfo()).
1307 1307 */
1308 1308 if (aplist_append(alpp, sdp,
1309 1309 AL_CNT_OFL_SYMINFOSYMS) == NULL)
1310 1310 return (0);
1311 1311
1312 1312 /*
1313 1313 * Flag that the symbol has a direct association
1314 1314 * with the external reference (this is an old
1315 1315 * tagging, that has no real effect by itself).
1316 1316 */
1317 1317 syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
1318 1318
1319 1319 /*
1320 1320 * Flag any lazy or deferred reference.
1321 1321 */
1322 1322 if (sdp->sd_flags & FLG_SY_LAZYLD)
1323 1323 syminfo[ndx].si_flags |=
1324 1324 SYMINFO_FLG_LAZYLOAD;
1325 1325 if (sdp->sd_flags & FLG_SY_DEFERRED)
1326 1326 syminfo[ndx].si_flags |=
1327 1327 SYMINFO_FLG_DEFERRED;
1328 1328
1329 1329 /*
1330 1330 * Enable direct symbol bindings if:
1331 1331 *
1332 1332 * - Symbol was identified with the DIRECT
1333 1333 * keyword in a mapfile.
1334 1334 *
1335 1335 * - Symbol reference has been bound to a
1336 1336 * dependency which was specified as
1337 1337 * requiring direct bindings with -zdirect.
1338 1338 *
1339 1339 * - All symbol references are required to
1340 1340 * use direct bindings via -Bdirect.
1341 1341 */
1342 1342 if (sdp->sd_flags & FLG_SY_DIR)
1343 1343 syminfo[ndx].si_flags |=
1344 1344 SYMINFO_FLG_DIRECTBIND;
1345 1345
1346 1346 } else if ((sdp->sd_flags & FLG_SY_EXTERN) &&
1347 1347 (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
1348 1348 /*
1349 1349 * If this symbol has been explicitly defined
1350 1350 * as external, and remains unresolved, mark
1351 1351 * it as external.
1352 1352 */
1353 1353 syminfo[ndx].si_boundto = SYMINFO_BT_EXTERN;
1354 1354
1355 1355 } else if ((sdp->sd_flags & FLG_SY_PARENT) &&
1356 1356 (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
1357 1357 /*
1358 1358 * If this symbol has been explicitly defined
1359 1359 * to be a reference to a parent object,
1360 1360 * indicate whether a direct binding should be
1361 1361 * established.
1362 1362 */
1363 1363 syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
1364 1364 syminfo[ndx].si_boundto = SYMINFO_BT_PARENT;
1365 1365 if (sdp->sd_flags & FLG_SY_DIR)
1366 1366 syminfo[ndx].si_flags |=
1367 1367 SYMINFO_FLG_DIRECTBIND;
1368 1368
1369 1369 } else if (sdp->sd_flags & FLG_SY_STDFLTR) {
1370 1370 /*
1371 1371 * A filter definition. Although this symbol
1372 1372 * can only be a stub, it might be necessary to
1373 1373 * prevent external direct bindings.
1374 1374 */
1375 1375 syminfo[ndx].si_flags |= SYMINFO_FLG_FILTER;
1376 1376 if (sdp->sd_flags & FLG_SY_NDIR)
1377 1377 syminfo[ndx].si_flags |=
1378 1378 SYMINFO_FLG_NOEXTDIRECT;
1379 1379
1380 1380 } else if (sdp->sd_flags & FLG_SY_AUXFLTR) {
1381 1381 /*
1382 1382 * An auxiliary filter definition. By nature,
1383 1383 * this definition is direct, in that should the
1384 1384 * filtee lookup fail, we'll fall back to this
1385 1385 * object. It may still be necessary to
1386 1386 * prevent external direct bindings.
1387 1387 */
1388 1388 syminfo[ndx].si_flags |= SYMINFO_FLG_AUXILIARY;
1389 1389 if (sdp->sd_flags & FLG_SY_NDIR)
1390 1390 syminfo[ndx].si_flags |=
1391 1391 SYMINFO_FLG_NOEXTDIRECT;
1392 1392
1393 1393 } else if ((sdp->sd_ref == REF_REL_NEED) &&
1394 1394 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1395 1395 /*
1396 1396 * This definition exists within the object
1397 1397 * being created. Provide a default boundto
1398 1398 * definition, which may be overridden later.
1399 1399 */
1400 1400 syminfo[ndx].si_boundto = SYMINFO_BT_NONE;
1401 1401
1402 1402 /*
1403 1403 * Indicate whether it is necessary to prevent
1404 1404 * external direct bindings.
1405 1405 */
1406 1406 if (sdp->sd_flags & FLG_SY_NDIR) {
1407 1407 syminfo[ndx].si_flags |=
1408 1408 SYMINFO_FLG_NOEXTDIRECT;
1409 1409 }
1410 1410
1411 1411 /*
1412 1412 * Indicate that this symbol is acting as an
1413 1413 * individual interposer.
1414 1414 */
1415 1415 if (sdp->sd_flags & FLG_SY_INTPOSE) {
1416 1416 syminfo[ndx].si_flags |=
1417 1417 SYMINFO_FLG_INTERPOSE;
1418 1418 }
1419 1419
1420 1420 /*
1421 1421 * Indicate that this symbol is deferred, and
1422 1422 * hence should not be bound to during BIND_NOW
1423 1423 * relocations.
1424 1424 */
1425 1425 if (sdp->sd_flags & FLG_SY_DEFERRED) {
1426 1426 syminfo[ndx].si_flags |=
1427 1427 SYMINFO_FLG_DEFERRED;
1428 1428 }
1429 1429
1430 1430 /*
1431 1431 * If external bindings are allowed, indicate
1432 1432 * the binding, and a direct binding if
1433 1433 * necessary.
1434 1434 */
1435 1435 if ((sdp->sd_flags & FLG_SY_NDIR) == 0) {
1436 1436 syminfo[ndx].si_flags |=
1437 1437 SYMINFO_FLG_DIRECT;
1438 1438
1439 1439 if (sdp->sd_flags & FLG_SY_DIR)
1440 1440 syminfo[ndx].si_flags |=
1441 1441 SYMINFO_FLG_DIRECTBIND;
1442 1442
1443 1443 /*
1444 1444 * Provide a default boundto definition,
1445 1445 * which may be overridden later.
1446 1446 */
1447 1447 syminfo[ndx].si_boundto =
1448 1448 SYMINFO_BT_SELF;
1449 1449 }
1450 1450
1451 1451 /*
1452 1452 * Indicate that this is a capabilities symbol.
1453 1453 * Note, that this identification only provides
1454 1454 * information regarding the symbol that is
1455 1455 * visible from elfdump(1) -y. The association
1456 1456 * of a symbol to its capabilities is derived
1457 1457 * from a .SUNW_capinfo entry.
1458 1458 */
1459 1459 if ((sdp->sd_flags & FLG_SY_CAP) &&
1460 1460 ofl->ofl_oscapinfo) {
1461 1461 syminfo[ndx].si_flags |=
1462 1462 SYMINFO_FLG_CAP;
1463 1463 }
1464 1464 }
1465 1465 }
1466 1466
1467 1467 /*
1468 1468 * Note that the `sym' value is reset to be one of the new
1469 1469 * symbol table entries. This symbol will be updated further
1470 1470 * depending on the type of the symbol. Process the .symtab
1471 1471 * first, followed by the .dynsym, thus the `sym' value will
1472 1472 * remain as the .dynsym value when the .dynsym is present.
1473 1473 * This ensures that any versioning symbols st_name value will
1474 1474 * be appropriate for the string table used by version
1475 1475 * entries.
1476 1476 */
1477 1477 if (enter_in_symtab) {
1478 1478 Word _symndx;
1479 1479
1480 1480 if (local)
1481 1481 _symndx = scopesym_ndx;
1482 1482 else
1483 1483 _symndx = symtab_ndx;
1484 1484
1485 1485 symtab[_symndx] = *sdp->sd_sym;
1486 1486 sdp->sd_sym = sym = &symtab[_symndx];
1487 1487 (void) st_setstring(strtab, name, &stoff);
1488 1488 sym->st_name = stoff;
1489 1489 }
1490 1490 if (dynlocal) {
1491 1491 ldynsym[ldynscopesym_ndx] = *sdp->sd_sym;
1492 1492 sdp->sd_sym = sym = &ldynsym[ldynscopesym_ndx];
1493 1493 (void) st_setstring(dynstr, name, &stoff);
1494 1494 ldynsym[ldynscopesym_ndx].st_name = stoff;
1495 1495 /* Add it to sort section if it qualifies */
1496 1496 ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
1497 1497 ldynscopesym_ndx);
1498 1498 }
1499 1499
1500 1500 if (dynsym && !local) {
1501 1501 dynsym[dynsym_ndx] = *sdp->sd_sym;
1502 1502
1503 1503 /*
1504 1504 * Provided this isn't an unnamed register symbol,
1505 1505 * update the symbols name and hash value.
1506 1506 */
1507 1507 if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1508 1508 dynsym[dynsym_ndx].st_name) {
1509 1509 (void) st_setstring(dynstr, name, &stoff);
1510 1510 dynsym[dynsym_ndx].st_name = stoff;
1511 1511
1512 1512 if (stoff) {
1513 1513 Word hashval, _hashndx;
1514 1514
1515 1515 hashval =
1516 1516 sap->sa_hash % ofl->ofl_hashbkts;
1517 1517
1518 1518 /* LINTED */
1519 1519 if (_hashndx = hashbkt[hashval]) {
1520 1520 while (hashchain[_hashndx]) {
1521 1521 _hashndx =
1522 1522 hashchain[_hashndx];
1523 1523 }
1524 1524 hashchain[_hashndx] =
1525 1525 sdp->sd_symndx;
1526 1526 } else {
1527 1527 hashbkt[hashval] =
1528 1528 sdp->sd_symndx;
1529 1529 }
1530 1530 }
1531 1531 }
1532 1532 sdp->sd_sym = sym = &dynsym[dynsym_ndx];
1533 1533
1534 1534 /*
1535 1535 * Add it to sort section if it qualifies.
1536 1536 * The indexes in that section are relative to the
1537 1537 * the adjacent SUNW_ldynsym/dymsym pair, so we
1538 1538 * add the number of items in SUNW_ldynsym to the
1539 1539 * dynsym index.
1540 1540 */
1541 1541 ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
1542 1542 ldynsym_cnt + dynsym_ndx);
1543 1543 }
1544 1544
1545 1545 if (!enter_in_symtab && (!dynsym || (local && !dynlocal))) {
1546 1546 if (!(sdp->sd_flags & FLG_SY_UPREQD))
1547 1547 continue;
1548 1548 sym = sdp->sd_sym;
1549 1549 } else
1550 1550 sdp->sd_flags &= ~FLG_SY_CLEAN;
1551 1551
1552 1552 /*
1553 1553 * If we have a weak data symbol for which we need the real
1554 1554 * symbol also, save this processing until later.
1555 1555 *
1556 1556 * The exception to this is if the weak/strong have PLT's
1557 1557 * assigned to them. In that case we don't do the post-weak
1558 1558 * processing because the PLT's must be maintained so that we
1559 1559 * can do 'interpositioning' on both of the symbols.
1560 1560 */
1561 1561 if ((sap->sa_linkndx) &&
1562 1562 (ELF_ST_BIND(sym->st_info) == STB_WEAK) &&
1563 1563 (!sap->sa_PLTndx)) {
1564 1564 Sym_desc *_sdp;
1565 1565
1566 1566 _sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
1567 1567
1568 1568 if (_sdp->sd_ref != REF_DYN_SEEN) {
1569 1569 Wk_desc wk;
1570 1570
1571 1571 if (enter_in_symtab) {
1572 1572 if (local) {
1573 1573 wk.wk_symtab =
1574 1574 &symtab[scopesym_ndx];
1575 1575 scopesym_ndx++;
1576 1576 } else {
1577 1577 wk.wk_symtab =
1578 1578 &symtab[symtab_ndx];
1579 1579 symtab_ndx++;
1580 1580 }
1581 1581 } else {
1582 1582 wk.wk_symtab = NULL;
1583 1583 }
1584 1584 if (dynsym) {
1585 1585 if (!local) {
1586 1586 wk.wk_dynsym =
1587 1587 &dynsym[dynsym_ndx];
1588 1588 dynsym_ndx++;
1589 1589 } else if (dynlocal) {
1590 1590 wk.wk_dynsym =
1591 1591 &ldynsym[ldynscopesym_ndx];
1592 1592 ldynscopesym_ndx++;
1593 1593 }
1594 1594 } else {
1595 1595 wk.wk_dynsym = NULL;
1596 1596 }
1597 1597 wk.wk_weak = sdp;
1598 1598 wk.wk_alias = _sdp;
1599 1599
1600 1600 if (alist_append(&weak, &wk,
1601 1601 sizeof (Wk_desc), AL_CNT_WEAK) == NULL)
1602 1602 return ((Addr)S_ERROR);
1603 1603
1604 1604 continue;
1605 1605 }
1606 1606 }
1607 1607
1608 1608 DBG_CALL(Dbg_syms_old(ofl, sdp));
1609 1609
1610 1610 spec = NULL;
1611 1611 /*
1612 1612 * assign new symbol value.
1613 1613 */
1614 1614 sectndx = sdp->sd_shndx;
1615 1615 if (sectndx == SHN_UNDEF) {
1616 1616 if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) &&
1617 1617 (sym->st_value != 0)) {
1618 1618 ld_eprintf(ofl, ERR_WARNING,
1619 1619 MSG_INTL(MSG_SYM_NOTNULL),
1620 1620 demangle(name), sdp->sd_file->ifl_name);
1621 1621 }
1622 1622
1623 1623 /*
1624 1624 * Undefined weak global, if we are generating a static
1625 1625 * executable, output as an absolute zero. Otherwise
1626 1626 * leave it as is, ld.so.1 will skip symbols of this
1627 1627 * type (this technique allows applications and
1628 1628 * libraries to test for the existence of a symbol as an
1629 1629 * indication of the presence or absence of certain
1630 1630 * functionality).
1631 1631 */
1632 1632 if (OFL_IS_STATIC_EXEC(ofl) &&
1633 1633 (ELF_ST_BIND(sym->st_info) == STB_WEAK)) {
1634 1634 sdp->sd_flags |= FLG_SY_SPECSEC;
1635 1635 sdp->sd_shndx = sectndx = SHN_ABS;
1636 1636 }
1637 1637 } else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1638 1638 (sectndx == SHN_COMMON)) {
1639 1639 /* COMMONs have already been processed */
1640 1640 /* EMPTY */
1641 1641 ;
1642 1642 } else {
1643 1643 if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1644 1644 (sectndx == SHN_ABS))
1645 1645 spec = sdp->sd_aux->sa_symspec;
1646 1646
1647 1647 /* LINTED */
1648 1648 if (sdp->sd_flags & FLG_SY_COMMEXP) {
1649 1649 /*
1650 1650 * This is (or was) a COMMON symbol which was
1651 1651 * processed above - no processing
1652 1652 * required here.
1653 1653 */
1654 1654 ;
1655 1655 } else if (sdp->sd_ref == REF_DYN_NEED) {
1656 1656 uchar_t type, bind;
1657 1657
1658 1658 sectndx = SHN_UNDEF;
1659 1659 sym->st_value = 0;
1660 1660 sym->st_size = 0;
1661 1661
1662 1662 /*
1663 1663 * Make sure this undefined symbol is returned
1664 1664 * to the same binding as was defined in the
1665 1665 * original relocatable object reference.
1666 1666 */
1667 1667 type = ELF_ST_TYPE(sym-> st_info);
1668 1668 if (sdp->sd_flags & FLG_SY_GLOBREF)
1669 1669 bind = STB_GLOBAL;
1670 1670 else
1671 1671 bind = STB_WEAK;
1672 1672
1673 1673 sym->st_info = ELF_ST_INFO(bind, type);
1674 1674
1675 1675 } else if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1676 1676 (sdp->sd_ref == REF_REL_NEED)) {
1677 1677 osp = sdp->sd_isc->is_osdesc;
1678 1678 /* LINTED */
1679 1679 sectndx = elf_ndxscn(osp->os_scn);
1680 1680
1681 1681 /*
1682 1682 * In an executable, the new symbol value is the
1683 1683 * old value (offset into defining section) plus
1684 1684 * virtual address of defining section. In a
1685 1685 * relocatable, the new value is the old value
1686 1686 * plus the displacement of the section within
1687 1687 * the file.
1688 1688 */
1689 1689 /* LINTED */
1690 1690 sym->st_value +=
1691 1691 (Off)_elf_getxoff(sdp->sd_isc->is_indata);
1692 1692
1693 1693 if (!(flags & FLG_OF_RELOBJ)) {
1694 1694 sym->st_value += osp->os_shdr->sh_addr;
1695 1695 /*
1696 1696 * TLS symbols are relative to
1697 1697 * the TLS segment.
1698 1698 */
1699 1699 if ((ELF_ST_TYPE(sym->st_info) ==
1700 1700 STT_TLS) && (ofl->ofl_tlsphdr))
1701 1701 sym->st_value -=
1702 1702 ofl->ofl_tlsphdr->p_vaddr;
1703 1703 }
1704 1704 }
1705 1705 }
1706 1706
1707 1707 if (spec) {
1708 1708 switch (spec) {
1709 1709 case SDAUX_ID_ETEXT:
1710 1710 sym->st_value = etext;
1711 1711 sectndx = etext_ndx;
1712 1712 if (etext_abs)
1713 1713 sdp->sd_flags |= FLG_SY_SPECSEC;
1714 1714 else
1715 1715 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1716 1716 break;
1717 1717 case SDAUX_ID_EDATA:
1718 1718 sym->st_value = edata;
1719 1719 sectndx = edata_ndx;
1720 1720 if (edata_abs)
1721 1721 sdp->sd_flags |= FLG_SY_SPECSEC;
1722 1722 else
1723 1723 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1724 1724 break;
1725 1725 case SDAUX_ID_END:
1726 1726 sym->st_value = end;
1727 1727 sectndx = end_ndx;
1728 1728 if (end_abs)
1729 1729 sdp->sd_flags |= FLG_SY_SPECSEC;
1730 1730 else
1731 1731 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1732 1732 break;
1733 1733 case SDAUX_ID_START:
1734 1734 sym->st_value = start;
1735 1735 sectndx = start_ndx;
1736 1736 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1737 1737 break;
1738 1738 case SDAUX_ID_DYN:
1739 1739 if (flags & FLG_OF_DYNAMIC) {
1740 1740 sym->st_value = ofl->
1741 1741 ofl_osdynamic->os_shdr->sh_addr;
1742 1742 /* LINTED */
1743 1743 sectndx = elf_ndxscn(
1744 1744 ofl->ofl_osdynamic->os_scn);
1745 1745 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1746 1746 }
1747 1747 break;
1748 1748 case SDAUX_ID_PLT:
1749 1749 if (ofl->ofl_osplt) {
1750 1750 sym->st_value = ofl->
1751 1751 ofl_osplt->os_shdr->sh_addr;
1752 1752 /* LINTED */
1753 1753 sectndx = elf_ndxscn(
1754 1754 ofl->ofl_osplt->os_scn);
1755 1755 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1756 1756 }
1757 1757 break;
1758 1758 case SDAUX_ID_GOT:
1759 1759 /*
1760 1760 * Symbol bias for negative growing tables is
1761 1761 * stored in symbol's value during
1762 1762 * allocate_got().
1763 1763 */
1764 1764 sym->st_value += ofl->
1765 1765 ofl_osgot->os_shdr->sh_addr;
1766 1766 /* LINTED */
1767 1767 sectndx = elf_ndxscn(ofl->
1768 1768 ofl_osgot->os_scn);
1769 1769 sdp->sd_flags &= ~FLG_SY_SPECSEC;
1770 1770 break;
1771 1771 default:
1772 1772 /* NOTHING */
1773 1773 ;
1774 1774 }
1775 1775 }
1776 1776
1777 1777 /*
1778 1778 * If a plt index has been assigned to an undefined function,
1779 1779 * update the symbols value to the appropriate .plt address.
1780 1780 */
1781 1781 if ((flags & FLG_OF_DYNAMIC) && (flags & FLG_OF_EXEC) &&
1782 1782 (sdp->sd_file) &&
1783 1783 (sdp->sd_file->ifl_ehdr->e_type == ET_DYN) &&
1784 1784 (ELF_ST_TYPE(sym->st_info) == STT_FUNC) &&
1785 1785 !(flags & FLG_OF_BFLAG)) {
1786 1786 if (sap->sa_PLTndx)
1787 1787 sym->st_value =
1788 1788 (*ld_targ.t_mr.mr_calc_plt_addr)(sdp, ofl);
1789 1789 }
1790 1790
1791 1791 /*
1792 1792 * Finish updating the symbols.
1793 1793 */
1794 1794
1795 1795 /*
1796 1796 * Sym Update: if scoped local - set local binding
1797 1797 */
1798 1798 if (local)
1799 1799 sym->st_info = ELF_ST_INFO(STB_LOCAL,
1800 1800 ELF_ST_TYPE(sym->st_info));
1801 1801
1802 1802 /*
1803 1803 * Sym Updated: If both the .symtab and .dynsym
1804 1804 * are present then we've actually updated the information in
1805 1805 * the .dynsym, therefore copy this same information to the
1806 1806 * .symtab entry.
1807 1807 */
1808 1808 sdp->sd_shndx = sectndx;
1809 1809 if (enter_in_symtab && dynsym && (!local || dynlocal)) {
1810 1810 Word _symndx = dynlocal ? scopesym_ndx : symtab_ndx;
1811 1811
1812 1812 symtab[_symndx].st_value = sym->st_value;
1813 1813 symtab[_symndx].st_size = sym->st_size;
1814 1814 symtab[_symndx].st_info = sym->st_info;
1815 1815 symtab[_symndx].st_other = sym->st_other;
1816 1816 }
1817 1817
1818 1818 if (enter_in_symtab) {
1819 1819 Word _symndx;
1820 1820
1821 1821 if (local)
1822 1822 _symndx = scopesym_ndx++;
1823 1823 else
1824 1824 _symndx = symtab_ndx++;
1825 1825 if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1826 1826 (sectndx >= SHN_LORESERVE)) {
1827 1827 assert(symshndx != NULL);
1828 1828 symshndx[_symndx] = sectndx;
1829 1829 symtab[_symndx].st_shndx = SHN_XINDEX;
1830 1830 } else {
1831 1831 /* LINTED */
1832 1832 symtab[_symndx].st_shndx = (Half)sectndx;
1833 1833 }
1834 1834 }
1835 1835
1836 1836 if (dynsym && (!local || dynlocal)) {
1837 1837 /*
1838 1838 * dynsym and ldynsym are distinct tables, so
1839 1839 * we use indirection to access the right one
1840 1840 * and the related extended section index array.
1841 1841 */
1842 1842 Word _symndx;
1843 1843 Sym *_dynsym;
1844 1844 Word *_dynshndx;
1845 1845
1846 1846 if (!local) {
1847 1847 _symndx = dynsym_ndx++;
1848 1848 _dynsym = dynsym;
1849 1849 _dynshndx = dynshndx;
1850 1850 } else {
1851 1851 _symndx = ldynscopesym_ndx++;
1852 1852 _dynsym = ldynsym;
1853 1853 _dynshndx = ldynshndx;
1854 1854 }
1855 1855 if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1856 1856 (sectndx >= SHN_LORESERVE)) {
1857 1857 assert(_dynshndx != NULL);
1858 1858 _dynshndx[_symndx] = sectndx;
1859 1859 _dynsym[_symndx].st_shndx = SHN_XINDEX;
1860 1860 } else {
1861 1861 /* LINTED */
1862 1862 _dynsym[_symndx].st_shndx = (Half)sectndx;
1863 1863 }
1864 1864 }
1865 1865
1866 1866 DBG_CALL(Dbg_syms_new(ofl, sym, sdp));
1867 1867 }
1868 1868
1869 1869 /*
1870 1870 * Now that all the symbols have been processed update any weak symbols
1871 1871 * information (ie. copy all information except `st_name'). As both
1872 1872 * symbols will be represented in the output, return the weak symbol to
1873 1873 * its correct type.
1874 1874 */
1875 1875 for (ALIST_TRAVERSE(weak, idx1, wkp)) {
1876 1876 Sym_desc *sdp, *_sdp;
1877 1877 Sym *sym, *_sym, *__sym;
1878 1878 uchar_t bind;
1879 1879
1880 1880 sdp = wkp->wk_weak;
1881 1881 _sdp = wkp->wk_alias;
1882 1882 _sym = __sym = _sdp->sd_sym;
1883 1883
1884 1884 sdp->sd_flags |= FLG_SY_WEAKDEF;
1885 1885
1886 1886 /*
1887 1887 * If the symbol definition has been scoped then assign it to
1888 1888 * be local, otherwise if it's from a shared object then we need
1889 1889 * to maintain the binding of the original reference.
1890 1890 */
1891 1891 if (SYM_IS_HIDDEN(sdp)) {
1892 1892 if (flags & FLG_OF_PROCRED)
1893 1893 bind = STB_LOCAL;
1894 1894 else
1895 1895 bind = STB_WEAK;
1896 1896 } else if ((sdp->sd_ref == REF_DYN_NEED) &&
1897 1897 (sdp->sd_flags & FLG_SY_GLOBREF))
1898 1898 bind = STB_GLOBAL;
1899 1899 else
1900 1900 bind = STB_WEAK;
1901 1901
1902 1902 DBG_CALL(Dbg_syms_old(ofl, sdp));
1903 1903 if ((sym = wkp->wk_symtab) != NULL) {
1904 1904 sym->st_value = _sym->st_value;
1905 1905 sym->st_size = _sym->st_size;
1906 1906 sym->st_other = _sym->st_other;
1907 1907 sym->st_shndx = _sym->st_shndx;
1908 1908 sym->st_info = ELF_ST_INFO(bind,
1909 1909 ELF_ST_TYPE(sym->st_info));
1910 1910 __sym = sym;
1911 1911 }
1912 1912 if ((sym = wkp->wk_dynsym) != NULL) {
1913 1913 sym->st_value = _sym->st_value;
1914 1914 sym->st_size = _sym->st_size;
1915 1915 sym->st_other = _sym->st_other;
1916 1916 sym->st_shndx = _sym->st_shndx;
1917 1917 sym->st_info = ELF_ST_INFO(bind,
1918 1918 ELF_ST_TYPE(sym->st_info));
1919 1919 __sym = sym;
1920 1920 }
1921 1921 DBG_CALL(Dbg_syms_new(ofl, __sym, sdp));
1922 1922 }
1923 1923
1924 1924 /*
1925 1925 * Now display GOT debugging information if required.
1926 1926 */
1927 1927 DBG_CALL(Dbg_got_display(ofl, 0, 0,
1928 1928 ld_targ.t_m.m_got_xnumber, ld_targ.t_m.m_got_entsize));
1929 1929
1930 1930 /*
1931 1931 * Update the section headers information. sh_info is
1932 1932 * supposed to contain the offset at which the first
1933 1933 * global symbol resides in the symbol table, while
1934 1934 * sh_link contains the section index of the associated
1935 1935 * string table.
1936 1936 */
1937 1937 if (symtab) {
1938 1938 Shdr *shdr = ofl->ofl_ossymtab->os_shdr;
1939 1939
1940 1940 shdr->sh_info = symtab_gbl_bndx;
1941 1941 /* LINTED */
1942 1942 shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osstrtab->os_scn);
1943 1943 if (symshndx)
1944 1944 ofl->ofl_ossymshndx->os_shdr->sh_link =
1945 1945 (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
1946 1946
1947 1947 /*
1948 1948 * Ensure that the expected number of symbols
1949 1949 * were entered into the right spots:
1950 1950 * - Scoped symbols in the right range
1951 1951 * - Globals start at the right spot
1952 1952 * (correct number of locals entered)
1953 1953 * - The table is exactly filled
1954 1954 * (correct number of globals entered)
1955 1955 */
1956 1956 assert((scopesym_bndx + ofl->ofl_scopecnt) == scopesym_ndx);
1957 1957 assert(shdr->sh_info == SYMTAB_LOC_CNT(ofl));
1958 1958 assert((shdr->sh_info + ofl->ofl_globcnt) == symtab_ndx);
1959 1959 }
1960 1960 if (dynsym) {
1961 1961 Shdr *shdr = ofl->ofl_osdynsym->os_shdr;
1962 1962
1963 1963 shdr->sh_info = DYNSYM_LOC_CNT(ofl);
1964 1964 /* LINTED */
1965 1965 shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
1966 1966
1967 1967 ofl->ofl_oshash->os_shdr->sh_link =
1968 1968 /* LINTED */
1969 1969 (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
1970 1970 if (dynshndx) {
1971 1971 shdr = ofl->ofl_osdynshndx->os_shdr;
1972 1972 shdr->sh_link =
1973 1973 (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
1974 1974 }
1975 1975 }
1976 1976 if (ldynsym) {
1977 1977 Shdr *shdr = ofl->ofl_osldynsym->os_shdr;
1978 1978
1979 1979 /* ldynsym has no globals, so give index one past the end */
1980 1980 shdr->sh_info = ldynsym_ndx;
1981 1981
1982 1982 /*
1983 1983 * The ldynsym and dynsym must be adjacent. The
1984 1984 * idea is that rtld should be able to start with
1985 1985 * the ldynsym and march straight through the end
1986 1986 * of dynsym, seeing them as a single symbol table,
1987 1987 * despite the fact that they are in distinct sections.
1988 1988 * Ensure that this happened correctly.
1989 1989 *
1990 1990 * Note that I use ldynsym_ndx here instead of the
1991 1991 * computation I used to set the section size
1992 1992 * (found in ldynsym_cnt). The two will agree, unless
1993 1993 * we somehow miscounted symbols or failed to insert them
1994 1994 * all. Using ldynsym_ndx here catches that error in
1995 1995 * addition to checking for adjacency.
1996 1996 */
1997 1997 assert(dynsym == (ldynsym + ldynsym_ndx));
1998 1998
1999 1999
2000 2000 /* LINTED */
2001 2001 shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
2002 2002
2003 2003 if (ldynshndx) {
2004 2004 shdr = ofl->ofl_osldynshndx->os_shdr;
2005 2005 shdr->sh_link =
2006 2006 (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
2007 2007 }
2008 2008
2009 2009 /*
2010 2010 * The presence of .SUNW_ldynsym means that there may be
2011 2011 * associated sort sections, one for regular symbols
2012 2012 * and the other for TLS. Each sort section needs the
2013 2013 * following done:
2014 2014 * - Section header link references .SUNW_ldynsym
2015 2015 * - Should have received the expected # of items
2016 2016 * - Sorted by increasing address
2017 2017 */
2018 2018 if (ofl->ofl_osdynsymsort) { /* .SUNW_dynsymsort */
2019 2019 ofl->ofl_osdynsymsort->os_shdr->sh_link =
2020 2020 (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
2021 2021 assert(ofl->ofl_dynsymsortcnt == dynsymsort_ndx);
2022 2022
2023 2023 if (dynsymsort_ndx > 1) {
2024 2024 dynsort_compare_syms = ldynsym;
2025 2025 qsort(dynsymsort, dynsymsort_ndx,
2026 2026 sizeof (*dynsymsort), dynsort_compare);
2027 2027 dynsort_dupwarn(ofl, ldynsym,
2028 2028 st_getstrbuf(dynstr),
2029 2029 dynsymsort, dynsymsort_ndx,
2030 2030 MSG_ORIG(MSG_SCN_DYNSYMSORT));
2031 2031 }
2032 2032 }
2033 2033 if (ofl->ofl_osdyntlssort) { /* .SUNW_dyntlssort */
2034 2034 ofl->ofl_osdyntlssort->os_shdr->sh_link =
2035 2035 (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
2036 2036 assert(ofl->ofl_dyntlssortcnt == dyntlssort_ndx);
2037 2037
2038 2038 if (dyntlssort_ndx > 1) {
2039 2039 dynsort_compare_syms = ldynsym;
2040 2040 qsort(dyntlssort, dyntlssort_ndx,
2041 2041 sizeof (*dyntlssort), dynsort_compare);
2042 2042 dynsort_dupwarn(ofl, ldynsym,
2043 2043 st_getstrbuf(dynstr),
2044 2044 dyntlssort, dyntlssort_ndx,
2045 2045 MSG_ORIG(MSG_SCN_DYNTLSSORT));
2046 2046 }
2047 2047 }
2048 2048 }
2049 2049
2050 2050 /*
2051 2051 * Used by ld.so.1 only.
2052 2052 */
2053 2053 return (etext);
2054 2054
2055 2055 #undef ADD_TO_DYNSORT
2056 2056 }
2057 2057
2058 2058 /*
2059 2059 * Build the dynamic section.
2060 2060 *
2061 2061 * This routine must be maintained in parallel with make_dynamic()
2062 2062 * in sections.c
2063 2063 */
2064 2064 static int
2065 2065 update_odynamic(Ofl_desc *ofl)
2066 2066 {
2067 2067 Aliste idx;
2068 2068 Ifl_desc *ifl;
2069 2069 Sym_desc *sdp;
2070 2070 Shdr *shdr;
2071 2071 Dyn *_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
2072 2072 Dyn *dyn;
2073 2073 Os_desc *symosp, *strosp;
2074 2074 Str_tbl *strtbl;
2075 2075 size_t stoff;
2076 2076 ofl_flag_t flags = ofl->ofl_flags;
2077 2077 int not_relobj = !(flags & FLG_OF_RELOBJ);
2078 2078 Word cnt;
2079 2079
2080 2080 /*
2081 2081 * Relocatable objects can be built with -r and -dy to trigger the
2082 2082 * creation of a .dynamic section. This model is used to create kernel
2083 2083 * device drivers. The .dynamic section provides a subset of userland
2084 2084 * .dynamic entries, typically entries such as DT_NEEDED and DT_RUNPATH.
2085 2085 *
2086 2086 * Within a dynamic object, any .dynamic string references are to the
2087 2087 * .dynstr table. Within a relocatable object, these strings can reside
2088 2088 * within the .strtab.
2089 2089 */
2090 2090 if (OFL_IS_STATIC_OBJ(ofl)) {
2091 2091 symosp = ofl->ofl_ossymtab;
2092 2092 strosp = ofl->ofl_osstrtab;
2093 2093 strtbl = ofl->ofl_strtab;
2094 2094 } else {
2095 2095 symosp = ofl->ofl_osdynsym;
2096 2096 strosp = ofl->ofl_osdynstr;
2097 2097 strtbl = ofl->ofl_dynstrtab;
2098 2098 }
2099 2099
2100 2100 /* LINTED */
2101 2101 ofl->ofl_osdynamic->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2102 2102
2103 2103 dyn = _dyn;
2104 2104
2105 2105 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
2106 2106 if ((ifl->ifl_flags &
2107 2107 (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
2108 2108 continue;
2109 2109
2110 2110 /*
2111 2111 * Create and set up the DT_POSFLAG_1 entry here if required.
2112 2112 */
2113 2113 if ((ifl->ifl_flags & MSK_IF_POSFLAG1) &&
2114 2114 (ifl->ifl_flags & FLG_IF_NEEDED) && not_relobj) {
2115 2115 dyn->d_tag = DT_POSFLAG_1;
2116 2116 if (ifl->ifl_flags & FLG_IF_LAZYLD)
2117 2117 dyn->d_un.d_val = DF_P1_LAZYLOAD;
2118 2118 if (ifl->ifl_flags & FLG_IF_GRPPRM)
2119 2119 dyn->d_un.d_val |= DF_P1_GROUPPERM;
2120 2120 if (ifl->ifl_flags & FLG_IF_DEFERRED)
2121 2121 dyn->d_un.d_val |= DF_P1_DEFERRED;
2122 2122 dyn++;
2123 2123 }
2124 2124
2125 2125 if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
2126 2126 dyn->d_tag = DT_NEEDED;
2127 2127 else
2128 2128 continue;
2129 2129
2130 2130 (void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
2131 2131 dyn->d_un.d_val = stoff;
2132 2132 /* LINTED */
2133 2133 ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
2134 2134 sizeof (Dyn));
2135 2135 dyn++;
2136 2136 }
2137 2137
2138 2138 if (not_relobj) {
2139 2139 if (ofl->ofl_dtsfltrs != NULL) {
2140 2140 Dfltr_desc *dftp;
2141 2141
2142 2142 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) {
2143 2143 if (dftp->dft_flag == FLG_SY_AUXFLTR)
2144 2144 dyn->d_tag = DT_SUNW_AUXILIARY;
2145 2145 else
2146 2146 dyn->d_tag = DT_SUNW_FILTER;
2147 2147
2148 2148 (void) st_setstring(strtbl, dftp->dft_str,
2149 2149 &stoff);
2150 2150 dyn->d_un.d_val = stoff;
2151 2151 dftp->dft_ndx = (Half)(((uintptr_t)dyn -
2152 2152 (uintptr_t)_dyn) / sizeof (Dyn));
2153 2153 dyn++;
2154 2154 }
2155 2155 }
2156 2156 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
2157 2157 SYM_NOHASH, 0, ofl)) != NULL) &&
2158 2158 (sdp->sd_ref == REF_REL_NEED) &&
2159 2159 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
2160 2160 dyn->d_tag = DT_INIT;
2161 2161 dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2162 2162 dyn++;
2163 2163 }
2164 2164 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
2165 2165 SYM_NOHASH, 0, ofl)) != NULL) &&
2166 2166 (sdp->sd_ref == REF_REL_NEED) &&
2167 2167 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
2168 2168 dyn->d_tag = DT_FINI;
2169 2169 dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2170 2170 dyn++;
2171 2171 }
2172 2172 if (ofl->ofl_soname) {
2173 2173 dyn->d_tag = DT_SONAME;
2174 2174 (void) st_setstring(strtbl, ofl->ofl_soname, &stoff);
2175 2175 dyn->d_un.d_val = stoff;
2176 2176 dyn++;
2177 2177 }
2178 2178 if (ofl->ofl_filtees) {
2179 2179 if (flags & FLG_OF_AUX) {
2180 2180 dyn->d_tag = DT_AUXILIARY;
2181 2181 } else {
2182 2182 dyn->d_tag = DT_FILTER;
2183 2183 }
2184 2184 (void) st_setstring(strtbl, ofl->ofl_filtees, &stoff);
2185 2185 dyn->d_un.d_val = stoff;
2186 2186 dyn++;
2187 2187 }
2188 2188 }
2189 2189
2190 2190 if (ofl->ofl_rpath) {
2191 2191 (void) st_setstring(strtbl, ofl->ofl_rpath, &stoff);
2192 2192 dyn->d_tag = DT_RUNPATH;
2193 2193 dyn->d_un.d_val = stoff;
2194 2194 dyn++;
2195 2195 dyn->d_tag = DT_RPATH;
2196 2196 dyn->d_un.d_val = stoff;
2197 2197 dyn++;
2198 2198 }
2199 2199
2200 2200 if (not_relobj) {
2201 2201 Aliste idx;
2202 2202 Sg_desc *sgp;
2203 2203
2204 2204 if (ofl->ofl_config) {
2205 2205 dyn->d_tag = DT_CONFIG;
2206 2206 (void) st_setstring(strtbl, ofl->ofl_config, &stoff);
2207 2207 dyn->d_un.d_val = stoff;
2208 2208 dyn++;
2209 2209 }
2210 2210 if (ofl->ofl_depaudit) {
2211 2211 dyn->d_tag = DT_DEPAUDIT;
2212 2212 (void) st_setstring(strtbl, ofl->ofl_depaudit, &stoff);
2213 2213 dyn->d_un.d_val = stoff;
2214 2214 dyn++;
2215 2215 }
2216 2216 if (ofl->ofl_audit) {
2217 2217 dyn->d_tag = DT_AUDIT;
2218 2218 (void) st_setstring(strtbl, ofl->ofl_audit, &stoff);
2219 2219 dyn->d_un.d_val = stoff;
2220 2220 dyn++;
2221 2221 }
2222 2222
2223 2223 dyn->d_tag = DT_HASH;
2224 2224 dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
2225 2225 dyn++;
2226 2226
2227 2227 shdr = strosp->os_shdr;
2228 2228 dyn->d_tag = DT_STRTAB;
2229 2229 dyn->d_un.d_ptr = shdr->sh_addr;
2230 2230 dyn++;
2231 2231
2232 2232 dyn->d_tag = DT_STRSZ;
2233 2233 dyn->d_un.d_ptr = shdr->sh_size;
2234 2234 dyn++;
2235 2235
2236 2236 /*
2237 2237 * Note, the shdr is set and used in the ofl->ofl_osldynsym case
2238 2238 * that follows.
2239 2239 */
2240 2240 shdr = symosp->os_shdr;
2241 2241 dyn->d_tag = DT_SYMTAB;
2242 2242 dyn->d_un.d_ptr = shdr->sh_addr;
2243 2243 dyn++;
2244 2244
2245 2245 dyn->d_tag = DT_SYMENT;
2246 2246 dyn->d_un.d_ptr = shdr->sh_entsize;
2247 2247 dyn++;
2248 2248
2249 2249 if (ofl->ofl_osldynsym) {
2250 2250 Shdr *lshdr = ofl->ofl_osldynsym->os_shdr;
2251 2251
2252 2252 /*
2253 2253 * We have arranged for the .SUNW_ldynsym data to be
2254 2254 * immediately in front of the .dynsym data.
2255 2255 * This means that you could start at the top
2256 2256 * of .SUNW_ldynsym and see the data for both tables
2257 2257 * without a break. This is the view we want to
2258 2258 * provide for DT_SUNW_SYMTAB, which is why we
2259 2259 * add the lengths together.
2260 2260 */
2261 2261 dyn->d_tag = DT_SUNW_SYMTAB;
2262 2262 dyn->d_un.d_ptr = lshdr->sh_addr;
2263 2263 dyn++;
2264 2264
2265 2265 dyn->d_tag = DT_SUNW_SYMSZ;
2266 2266 dyn->d_un.d_val = lshdr->sh_size + shdr->sh_size;
2267 2267 dyn++;
2268 2268 }
2269 2269
2270 2270 if (ofl->ofl_osdynsymsort || ofl->ofl_osdyntlssort) {
2271 2271 dyn->d_tag = DT_SUNW_SORTENT;
2272 2272 dyn->d_un.d_val = sizeof (Word);
2273 2273 dyn++;
2274 2274 }
2275 2275
2276 2276 if (ofl->ofl_osdynsymsort) {
2277 2277 shdr = ofl->ofl_osdynsymsort->os_shdr;
2278 2278
2279 2279 dyn->d_tag = DT_SUNW_SYMSORT;
2280 2280 dyn->d_un.d_ptr = shdr->sh_addr;
2281 2281 dyn++;
2282 2282
2283 2283 dyn->d_tag = DT_SUNW_SYMSORTSZ;
2284 2284 dyn->d_un.d_val = shdr->sh_size;
2285 2285 dyn++;
2286 2286 }
2287 2287
2288 2288 if (ofl->ofl_osdyntlssort) {
2289 2289 shdr = ofl->ofl_osdyntlssort->os_shdr;
2290 2290
2291 2291 dyn->d_tag = DT_SUNW_TLSSORT;
2292 2292 dyn->d_un.d_ptr = shdr->sh_addr;
2293 2293 dyn++;
2294 2294
2295 2295 dyn->d_tag = DT_SUNW_TLSSORTSZ;
2296 2296 dyn->d_un.d_val = shdr->sh_size;
2297 2297 dyn++;
2298 2298 }
2299 2299
2300 2300 /*
2301 2301 * Reserve the DT_CHECKSUM entry. Its value will be filled in
2302 2302 * after the complete image is built.
2303 2303 */
2304 2304 dyn->d_tag = DT_CHECKSUM;
2305 2305 ofl->ofl_checksum = &dyn->d_un.d_val;
2306 2306 dyn++;
2307 2307
2308 2308 /*
2309 2309 * Versioning sections: DT_VERDEF and DT_VERNEED.
2310 2310 *
2311 2311 * The Solaris ld does not produce DT_VERSYM, but the GNU ld
2312 2312 * does, in order to support their style of versioning, which
2313 2313 * differs from ours:
2314 2314 *
2315 2315 * - The top bit of the 16-bit Versym index is
2316 2316 * not part of the version, but is interpreted
2317 2317 * as a "hidden bit".
2318 2318 *
2319 2319 * - External (SHN_UNDEF) symbols can have non-zero
2320 2320 * Versym values, which specify versions in
2321 2321 * referenced objects, via the Verneed section.
2322 2322 *
2323 2323 * - The vna_other field of the Vernaux structures
2324 2324 * found in the Verneed section are not zero as
2325 2325 * with Solaris, but instead contain the version
2326 2326 * index to be used by Versym indices to reference
2327 2327 * the given external version.
2328 2328 *
2329 2329 * The Solaris ld, rtld, and elfdump programs all interpret the
2330 2330 * presence of DT_VERSYM as meaning that GNU versioning rules
2331 2331 * apply to the given file. If DT_VERSYM is not present,
2332 2332 * then Solaris versioning rules apply. If we should ever need
2333 2333 * to change our ld so that it does issue DT_VERSYM, then
2334 2334 * this rule for detecting GNU versioning will no longer work.
2335 2335 * In that case, we will have to invent a way to explicitly
2336 2336 * specify the style of versioning in use, perhaps via a
2337 2337 * new dynamic entry named something like DT_SUNW_VERSIONSTYLE,
2338 2338 * where the d_un.d_val value specifies which style is to be
2339 2339 * used.
2340 2340 */
2341 2341 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
2342 2342 FLG_OF_VERDEF) {
2343 2343 shdr = ofl->ofl_osverdef->os_shdr;
2344 2344
2345 2345 dyn->d_tag = DT_VERDEF;
2346 2346 dyn->d_un.d_ptr = shdr->sh_addr;
2347 2347 dyn++;
2348 2348 dyn->d_tag = DT_VERDEFNUM;
2349 2349 dyn->d_un.d_ptr = shdr->sh_info;
2350 2350 dyn++;
2351 2351 }
2352 2352 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
2353 2353 FLG_OF_VERNEED) {
2354 2354 shdr = ofl->ofl_osverneed->os_shdr;
2355 2355
2356 2356 dyn->d_tag = DT_VERNEED;
2357 2357 dyn->d_un.d_ptr = shdr->sh_addr;
2358 2358 dyn++;
2359 2359 dyn->d_tag = DT_VERNEEDNUM;
2360 2360 dyn->d_un.d_ptr = shdr->sh_info;
2361 2361 dyn++;
2362 2362 }
2363 2363
2364 2364 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) {
2365 2365 dyn->d_tag = ld_targ.t_m.m_rel_dt_count;
2366 2366 dyn->d_un.d_val = ofl->ofl_relocrelcnt;
2367 2367 dyn++;
2368 2368 }
2369 2369 if (flags & FLG_OF_TEXTREL) {
2370 2370 /*
2371 2371 * Only the presence of this entry is used in this
2372 2372 * implementation, not the value stored.
2373 2373 */
2374 2374 dyn->d_tag = DT_TEXTREL;
2375 2375 dyn->d_un.d_val = 0;
2376 2376 dyn++;
2377 2377 }
2378 2378
2379 2379 if (ofl->ofl_osfiniarray) {
2380 2380 shdr = ofl->ofl_osfiniarray->os_shdr;
2381 2381
2382 2382 dyn->d_tag = DT_FINI_ARRAY;
2383 2383 dyn->d_un.d_ptr = shdr->sh_addr;
2384 2384 dyn++;
2385 2385
2386 2386 dyn->d_tag = DT_FINI_ARRAYSZ;
2387 2387 dyn->d_un.d_val = shdr->sh_size;
2388 2388 dyn++;
2389 2389 }
2390 2390
2391 2391 if (ofl->ofl_osinitarray) {
2392 2392 shdr = ofl->ofl_osinitarray->os_shdr;
2393 2393
2394 2394 dyn->d_tag = DT_INIT_ARRAY;
2395 2395 dyn->d_un.d_ptr = shdr->sh_addr;
2396 2396 dyn++;
2397 2397
2398 2398 dyn->d_tag = DT_INIT_ARRAYSZ;
2399 2399 dyn->d_un.d_val = shdr->sh_size;
2400 2400 dyn++;
2401 2401 }
2402 2402
2403 2403 if (ofl->ofl_ospreinitarray) {
2404 2404 shdr = ofl->ofl_ospreinitarray->os_shdr;
2405 2405
2406 2406 dyn->d_tag = DT_PREINIT_ARRAY;
2407 2407 dyn->d_un.d_ptr = shdr->sh_addr;
2408 2408 dyn++;
2409 2409
2410 2410 dyn->d_tag = DT_PREINIT_ARRAYSZ;
2411 2411 dyn->d_un.d_val = shdr->sh_size;
2412 2412 dyn++;
2413 2413 }
2414 2414
2415 2415 if (ofl->ofl_pltcnt) {
2416 2416 shdr = ofl->ofl_osplt->os_relosdesc->os_shdr;
2417 2417
2418 2418 dyn->d_tag = DT_PLTRELSZ;
2419 2419 dyn->d_un.d_ptr = shdr->sh_size;
2420 2420 dyn++;
2421 2421 dyn->d_tag = DT_PLTREL;
2422 2422 dyn->d_un.d_ptr = ld_targ.t_m.m_rel_dt_type;
2423 2423 dyn++;
2424 2424 dyn->d_tag = DT_JMPREL;
2425 2425 dyn->d_un.d_ptr = shdr->sh_addr;
2426 2426 dyn++;
2427 2427 }
2428 2428 if (ofl->ofl_pltpad) {
2429 2429 shdr = ofl->ofl_osplt->os_shdr;
2430 2430
2431 2431 dyn->d_tag = DT_PLTPAD;
2432 2432 if (ofl->ofl_pltcnt) {
2433 2433 dyn->d_un.d_ptr = shdr->sh_addr +
2434 2434 ld_targ.t_m.m_plt_reservsz +
2435 2435 ofl->ofl_pltcnt * ld_targ.t_m.m_plt_entsize;
2436 2436 } else
2437 2437 dyn->d_un.d_ptr = shdr->sh_addr;
2438 2438 dyn++;
2439 2439 dyn->d_tag = DT_PLTPADSZ;
2440 2440 dyn->d_un.d_val = ofl->ofl_pltpad *
2441 2441 ld_targ.t_m.m_plt_entsize;
2442 2442 dyn++;
2443 2443 }
2444 2444 if (ofl->ofl_relocsz) {
2445 2445 shdr = ofl->ofl_osrelhead->os_shdr;
2446 2446
2447 2447 dyn->d_tag = ld_targ.t_m.m_rel_dt_type;
2448 2448 dyn->d_un.d_ptr = shdr->sh_addr;
2449 2449 dyn++;
2450 2450 dyn->d_tag = ld_targ.t_m.m_rel_dt_size;
2451 2451 dyn->d_un.d_ptr = ofl->ofl_relocsz;
2452 2452 dyn++;
2453 2453 dyn->d_tag = ld_targ.t_m.m_rel_dt_ent;
2454 2454 if (shdr->sh_type == SHT_REL)
2455 2455 dyn->d_un.d_ptr = sizeof (Rel);
2456 2456 else
2457 2457 dyn->d_un.d_ptr = sizeof (Rela);
2458 2458 dyn++;
2459 2459 }
2460 2460 if (ofl->ofl_ossyminfo) {
2461 2461 shdr = ofl->ofl_ossyminfo->os_shdr;
2462 2462
2463 2463 dyn->d_tag = DT_SYMINFO;
2464 2464 dyn->d_un.d_ptr = shdr->sh_addr;
2465 2465 dyn++;
2466 2466 dyn->d_tag = DT_SYMINSZ;
2467 2467 dyn->d_un.d_val = shdr->sh_size;
2468 2468 dyn++;
2469 2469 dyn->d_tag = DT_SYMINENT;
2470 2470 dyn->d_un.d_val = sizeof (Syminfo);
2471 2471 dyn++;
2472 2472 }
2473 2473 if (ofl->ofl_osmove) {
2474 2474 shdr = ofl->ofl_osmove->os_shdr;
2475 2475
2476 2476 dyn->d_tag = DT_MOVETAB;
2477 2477 dyn->d_un.d_val = shdr->sh_addr;
2478 2478 dyn++;
2479 2479 dyn->d_tag = DT_MOVESZ;
2480 2480 dyn->d_un.d_val = shdr->sh_size;
2481 2481 dyn++;
2482 2482 dyn->d_tag = DT_MOVEENT;
2483 2483 dyn->d_un.d_val = shdr->sh_entsize;
2484 2484 dyn++;
2485 2485 }
2486 2486 if (ofl->ofl_regsymcnt) {
2487 2487 int ndx;
2488 2488
2489 2489 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2490 2490 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2491 2491 continue;
2492 2492
2493 2493 dyn->d_tag = ld_targ.t_m.m_dt_register;
2494 2494 dyn->d_un.d_val = sdp->sd_symndx;
2495 2495 dyn++;
2496 2496 }
2497 2497 }
2498 2498
2499 2499 for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp)) {
2500 2500 dyn->d_tag = DT_SUNW_RTLDINF;
2501 2501 dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2502 2502 dyn++;
2503 2503 }
2504 2504
2505 2505 if (((sgp = ofl->ofl_osdynamic->os_sgdesc) != NULL) &&
2506 2506 (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp) {
2507 2507 dyn->d_tag = DT_DEBUG;
2508 2508 dyn->d_un.d_ptr = 0;
2509 2509 dyn++;
2510 2510 }
2511 2511
2512 2512 if (ofl->ofl_oscap) {
2513 2513 dyn->d_tag = DT_SUNW_CAP;
2514 2514 dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
2515 2515 dyn++;
2516 2516 }
2517 2517 if (ofl->ofl_oscapinfo) {
2518 2518 dyn->d_tag = DT_SUNW_CAPINFO;
2519 2519 dyn->d_un.d_val = ofl->ofl_oscapinfo->os_shdr->sh_addr;
2520 2520 dyn++;
2521 2521 }
2522 2522 if (ofl->ofl_oscapchain) {
2523 2523 shdr = ofl->ofl_oscapchain->os_shdr;
2524 2524
↓ open down ↓ |
2524 lines elided |
↑ open up ↑ |
2525 2525 dyn->d_tag = DT_SUNW_CAPCHAIN;
2526 2526 dyn->d_un.d_val = shdr->sh_addr;
2527 2527 dyn++;
2528 2528 dyn->d_tag = DT_SUNW_CAPCHAINSZ;
2529 2529 dyn->d_un.d_val = shdr->sh_size;
2530 2530 dyn++;
2531 2531 dyn->d_tag = DT_SUNW_CAPCHAINENT;
2532 2532 dyn->d_un.d_val = shdr->sh_entsize;
2533 2533 dyn++;
2534 2534 }
2535 +
2536 + if (ofl->ofl_aslr != 0) {
2537 + dyn->d_tag = DT_SUNW_ASLR;
2538 + dyn->d_un.d_val = (ofl->ofl_aslr == 1);
2539 + dyn++;
2540 + }
2541 +
2535 2542 if (flags & FLG_OF_SYMBOLIC) {
2536 2543 dyn->d_tag = DT_SYMBOLIC;
2537 2544 dyn->d_un.d_val = 0;
2538 2545 dyn++;
2539 2546 }
2540 2547 }
2541 2548
2542 2549 dyn->d_tag = DT_FLAGS;
2543 2550 dyn->d_un.d_val = ofl->ofl_dtflags;
2544 2551 dyn++;
2545 2552
2546 2553 /*
2547 2554 * If -Bdirect was specified, but some NODIRECT symbols were specified
2548 2555 * via a mapfile, or -znodirect was used on the command line, then
2549 2556 * clear the DF_1_DIRECT flag. The resultant object will use per-symbol
2550 2557 * direct bindings rather than be enabled for global direct bindings.
2551 2558 *
2552 2559 * If any no-direct bindings exist within this object, set the
2553 2560 * DF_1_NODIRECT flag. ld(1) recognizes this flag when processing
2554 2561 * dependencies, and performs extra work to ensure that no direct
2555 2562 * bindings are established to the no-direct symbols that exist
2556 2563 * within these dependencies.
2557 2564 */
2558 2565 if (ofl->ofl_flags1 & FLG_OF1_NGLBDIR)
2559 2566 ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
2560 2567 if (ofl->ofl_flags1 & FLG_OF1_NDIRECT)
2561 2568 ofl->ofl_dtflags_1 |= DF_1_NODIRECT;
2562 2569
2563 2570 dyn->d_tag = DT_FLAGS_1;
2564 2571 dyn->d_un.d_val = ofl->ofl_dtflags_1;
2565 2572 dyn++;
2566 2573
2567 2574 dyn->d_tag = DT_SUNW_STRPAD;
2568 2575 dyn->d_un.d_val = DYNSTR_EXTRA_PAD;
2569 2576 dyn++;
2570 2577
2571 2578 dyn->d_tag = DT_SUNW_LDMACH;
2572 2579 dyn->d_un.d_val = ld_sunw_ldmach();
2573 2580 dyn++;
2574 2581
2575 2582 (*ld_targ.t_mr.mr_mach_update_odynamic)(ofl, &dyn);
2576 2583
2577 2584 for (cnt = 1 + DYNAMIC_EXTRA_ELTS; cnt--; dyn++) {
2578 2585 dyn->d_tag = DT_NULL;
2579 2586 dyn->d_un.d_val = 0;
2580 2587 }
2581 2588
2582 2589 /*
2583 2590 * Ensure that we wrote the right number of entries. If not, we either
2584 2591 * miscounted in make_dynamic(), or we did something wrong in this
2585 2592 * function.
2586 2593 */
2587 2594 assert((ofl->ofl_osdynamic->os_shdr->sh_size /
2588 2595 ofl->ofl_osdynamic->os_shdr->sh_entsize) ==
2589 2596 ((uintptr_t)dyn - (uintptr_t)_dyn) / sizeof (*dyn));
2590 2597
2591 2598 return (1);
2592 2599 }
2593 2600
2594 2601 /*
2595 2602 * Build the version definition section
2596 2603 */
2597 2604 static int
2598 2605 update_overdef(Ofl_desc *ofl)
2599 2606 {
2600 2607 Aliste idx1;
2601 2608 Ver_desc *vdp, *_vdp;
2602 2609 Verdef *vdf, *_vdf;
2603 2610 int num = 0;
2604 2611 Os_desc *strosp;
2605 2612 Str_tbl *strtbl;
2606 2613
2607 2614 /*
2608 2615 * Determine which string table to use.
2609 2616 */
2610 2617 if (OFL_IS_STATIC_OBJ(ofl)) {
2611 2618 strtbl = ofl->ofl_strtab;
2612 2619 strosp = ofl->ofl_osstrtab;
2613 2620 } else {
2614 2621 strtbl = ofl->ofl_dynstrtab;
2615 2622 strosp = ofl->ofl_osdynstr;
2616 2623 }
2617 2624
2618 2625 /*
2619 2626 * Traverse the version descriptors and update the version structures
2620 2627 * to point to the dynstr name in preparation for building the version
2621 2628 * section structure.
2622 2629 */
2623 2630 for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
2624 2631 Sym_desc *sdp;
2625 2632
2626 2633 if (vdp->vd_flags & VER_FLG_BASE) {
2627 2634 const char *name = vdp->vd_name;
2628 2635 size_t stoff;
2629 2636
2630 2637 /*
2631 2638 * Create a new string table entry to represent the base
2632 2639 * version name (there is no corresponding symbol for
2633 2640 * this).
2634 2641 */
2635 2642 (void) st_setstring(strtbl, name, &stoff);
2636 2643 /* LINTED */
2637 2644 vdp->vd_name = (const char *)stoff;
2638 2645 } else {
2639 2646 sdp = ld_sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
2640 2647 /* LINTED */
2641 2648 vdp->vd_name = (const char *)
2642 2649 (uintptr_t)sdp->sd_sym->st_name;
2643 2650 }
2644 2651 }
2645 2652
2646 2653 _vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;
2647 2654
2648 2655 /*
2649 2656 * Traverse the version descriptors and update the version section to
2650 2657 * reflect each version and its associated dependencies.
2651 2658 */
2652 2659 for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
2653 2660 Aliste idx2;
2654 2661 Half cnt = 1;
2655 2662 Verdaux *vdap, *_vdap;
2656 2663
2657 2664 _vdap = vdap = (Verdaux *)(vdf + 1);
2658 2665
2659 2666 vdf->vd_version = VER_DEF_CURRENT;
2660 2667 vdf->vd_flags = vdp->vd_flags & MSK_VER_USER;
2661 2668 vdf->vd_ndx = vdp->vd_ndx;
2662 2669 vdf->vd_hash = vdp->vd_hash;
2663 2670
2664 2671 /* LINTED */
2665 2672 vdap->vda_name = (uintptr_t)vdp->vd_name;
2666 2673 vdap++;
2667 2674 /* LINTED */
2668 2675 _vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);
2669 2676
2670 2677 /*
2671 2678 * Traverse this versions dependency list generating the
2672 2679 * appropriate version dependency entries.
2673 2680 */
2674 2681 for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp)) {
2675 2682 /* LINTED */
2676 2683 vdap->vda_name = (uintptr_t)_vdp->vd_name;
2677 2684 _vdap = vdap;
2678 2685 vdap++, cnt++;
2679 2686 /* LINTED */
2680 2687 _vdap->vda_next = (Word)((uintptr_t)vdap -
2681 2688 (uintptr_t)_vdap);
2682 2689 }
2683 2690 _vdap->vda_next = 0;
2684 2691
2685 2692 /*
2686 2693 * Record the versions auxiliary array offset and the associated
2687 2694 * dependency count.
2688 2695 */
2689 2696 /* LINTED */
2690 2697 vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
2691 2698 vdf->vd_cnt = cnt;
2692 2699
2693 2700 /*
2694 2701 * Record the next versions offset and update the version
2695 2702 * pointer. Remember the previous version offset as the very
2696 2703 * last structures next pointer should be null.
2697 2704 */
2698 2705 _vdf = vdf;
2699 2706 vdf = (Verdef *)vdap, num++;
2700 2707 /* LINTED */
2701 2708 _vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
2702 2709 }
2703 2710 _vdf->vd_next = 0;
2704 2711
2705 2712 /*
2706 2713 * Record the string table association with the version definition
2707 2714 * section, and the symbol table associated with the version symbol
2708 2715 * table (the actual contents of the version symbol table are filled
2709 2716 * in during symbol update).
2710 2717 */
2711 2718 /* LINTED */
2712 2719 ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2713 2720
2714 2721 /*
2715 2722 * The version definition sections `info' field is used to indicate the
2716 2723 * number of entries in this section.
2717 2724 */
2718 2725 ofl->ofl_osverdef->os_shdr->sh_info = num;
2719 2726
2720 2727 return (1);
2721 2728 }
2722 2729
2723 2730 /*
2724 2731 * Finish the version symbol index section
2725 2732 */
2726 2733 static void
2727 2734 update_oversym(Ofl_desc *ofl)
2728 2735 {
2729 2736 Os_desc *osp;
2730 2737
2731 2738 /*
2732 2739 * Record the symbol table associated with the version symbol table.
2733 2740 * The contents of the version symbol table are filled in during
2734 2741 * symbol update.
2735 2742 */
2736 2743 if (OFL_IS_STATIC_OBJ(ofl))
2737 2744 osp = ofl->ofl_ossymtab;
2738 2745 else
2739 2746 osp = ofl->ofl_osdynsym;
2740 2747
2741 2748 /* LINTED */
2742 2749 ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(osp->os_scn);
2743 2750 }
2744 2751
2745 2752 /*
2746 2753 * Build the version needed section
2747 2754 */
2748 2755 static int
2749 2756 update_overneed(Ofl_desc *ofl)
2750 2757 {
2751 2758 Aliste idx1;
2752 2759 Ifl_desc *ifl;
2753 2760 Verneed *vnd, *_vnd;
2754 2761 Os_desc *strosp;
2755 2762 Str_tbl *strtbl;
2756 2763 Word num = 0;
2757 2764
2758 2765 _vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;
2759 2766
2760 2767 /*
2761 2768 * Determine which string table is appropriate.
2762 2769 */
2763 2770 if (OFL_IS_STATIC_OBJ(ofl)) {
2764 2771 strosp = ofl->ofl_osstrtab;
2765 2772 strtbl = ofl->ofl_strtab;
2766 2773 } else {
2767 2774 strosp = ofl->ofl_osdynstr;
2768 2775 strtbl = ofl->ofl_dynstrtab;
2769 2776 }
2770 2777
2771 2778 /*
2772 2779 * Traverse the shared object list looking for dependencies that have
2773 2780 * versions defined within them.
2774 2781 */
2775 2782 for (APLIST_TRAVERSE(ofl->ofl_sos, idx1, ifl)) {
2776 2783 Half _cnt;
2777 2784 Word cnt = 0;
2778 2785 Vernaux *_vnap, *vnap;
2779 2786 size_t stoff;
2780 2787
2781 2788 if (!(ifl->ifl_flags & FLG_IF_VERNEED))
2782 2789 continue;
2783 2790
2784 2791 vnd->vn_version = VER_NEED_CURRENT;
2785 2792
2786 2793 (void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
2787 2794 vnd->vn_file = stoff;
2788 2795
2789 2796 _vnap = vnap = (Vernaux *)(vnd + 1);
2790 2797
2791 2798 /*
2792 2799 * Traverse the version index list recording
2793 2800 * each version as a needed dependency.
2794 2801 */
2795 2802 for (_cnt = 0; _cnt <= ifl->ifl_vercnt; _cnt++) {
2796 2803 Ver_index *vip = &ifl->ifl_verndx[_cnt];
2797 2804
2798 2805 if (vip->vi_flags & FLG_VER_REFER) {
2799 2806 (void) st_setstring(strtbl, vip->vi_name,
2800 2807 &stoff);
2801 2808 vnap->vna_name = stoff;
2802 2809
2803 2810 if (vip->vi_desc) {
2804 2811 vnap->vna_hash = vip->vi_desc->vd_hash;
2805 2812 vnap->vna_flags =
2806 2813 vip->vi_desc->vd_flags;
2807 2814 } else {
2808 2815 vnap->vna_hash = 0;
2809 2816 vnap->vna_flags = 0;
2810 2817 }
2811 2818 vnap->vna_other = vip->vi_overndx;
2812 2819
2813 2820 /*
2814 2821 * If version A inherits version B, then
2815 2822 * B is implicit in A. It suffices for ld.so.1
2816 2823 * to verify A at runtime and skip B. The
2817 2824 * version normalization process sets the INFO
2818 2825 * flag for the versions we want ld.so.1 to
2819 2826 * skip.
2820 2827 */
2821 2828 if (vip->vi_flags & VER_FLG_INFO)
2822 2829 vnap->vna_flags |= VER_FLG_INFO;
2823 2830
2824 2831 _vnap = vnap;
2825 2832 vnap++, cnt++;
2826 2833 _vnap->vna_next =
2827 2834 /* LINTED */
2828 2835 (Word)((uintptr_t)vnap - (uintptr_t)_vnap);
2829 2836 }
2830 2837 }
2831 2838
2832 2839 _vnap->vna_next = 0;
2833 2840
2834 2841 /*
2835 2842 * Record the versions auxiliary array offset and
2836 2843 * the associated dependency count.
2837 2844 */
2838 2845 /* LINTED */
2839 2846 vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
2840 2847 /* LINTED */
2841 2848 vnd->vn_cnt = (Half)cnt;
2842 2849
2843 2850 /*
2844 2851 * Record the next versions offset and update the version
2845 2852 * pointer. Remember the previous version offset as the very
2846 2853 * last structures next pointer should be null.
2847 2854 */
2848 2855 _vnd = vnd;
2849 2856 vnd = (Verneed *)vnap, num++;
2850 2857 /* LINTED */
2851 2858 _vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
2852 2859 }
2853 2860 _vnd->vn_next = 0;
2854 2861
2855 2862 /*
2856 2863 * Use sh_link to record the associated string table section, and
2857 2864 * sh_info to indicate the number of entries contained in the section.
2858 2865 */
2859 2866 /* LINTED */
2860 2867 ofl->ofl_osverneed->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2861 2868 ofl->ofl_osverneed->os_shdr->sh_info = num;
2862 2869
2863 2870 return (1);
2864 2871 }
2865 2872
2866 2873 /*
2867 2874 * Update syminfo section.
2868 2875 */
2869 2876 static uintptr_t
2870 2877 update_osyminfo(Ofl_desc *ofl)
2871 2878 {
2872 2879 Os_desc *symosp, *infosp = ofl->ofl_ossyminfo;
2873 2880 Syminfo *sip = infosp->os_outdata->d_buf;
2874 2881 Shdr *shdr = infosp->os_shdr;
2875 2882 char *strtab;
2876 2883 Aliste idx;
2877 2884 Sym_desc *sdp;
2878 2885 Sfltr_desc *sftp;
2879 2886
2880 2887 if (ofl->ofl_flags & FLG_OF_RELOBJ) {
2881 2888 symosp = ofl->ofl_ossymtab;
2882 2889 strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
2883 2890 } else {
2884 2891 symosp = ofl->ofl_osdynsym;
2885 2892 strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
2886 2893 }
2887 2894
2888 2895 /* LINTED */
2889 2896 infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2890 2897 if (ofl->ofl_osdynamic)
2891 2898 infosp->os_shdr->sh_info =
2892 2899 /* LINTED */
2893 2900 (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);
2894 2901
2895 2902 /*
2896 2903 * Update any references with the index into the dynamic table.
2897 2904 */
2898 2905 for (APLIST_TRAVERSE(ofl->ofl_symdtent, idx, sdp))
2899 2906 sip[sdp->sd_symndx].si_boundto = sdp->sd_file->ifl_neededndx;
2900 2907
2901 2908 /*
2902 2909 * Update any filtee references with the index into the dynamic table.
2903 2910 */
2904 2911 for (ALIST_TRAVERSE(ofl->ofl_symfltrs, idx, sftp)) {
2905 2912 Dfltr_desc *dftp;
2906 2913
2907 2914 dftp = alist_item(ofl->ofl_dtsfltrs, sftp->sft_idx);
2908 2915 sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
2909 2916 }
2910 2917
2911 2918 /*
2912 2919 * Display debugging information about section.
2913 2920 */
2914 2921 DBG_CALL(Dbg_syminfo_title(ofl->ofl_lml));
2915 2922 if (DBG_ENABLED) {
2916 2923 Word _cnt, cnt = shdr->sh_size / shdr->sh_entsize;
2917 2924 Sym *symtab = symosp->os_outdata->d_buf;
2918 2925 Dyn *dyn;
2919 2926
2920 2927 if (ofl->ofl_osdynamic)
2921 2928 dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
2922 2929 else
2923 2930 dyn = NULL;
2924 2931
2925 2932 for (_cnt = 1; _cnt < cnt; _cnt++) {
2926 2933 if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
2927 2934 /* LINTED */
2928 2935 DBG_CALL(Dbg_syminfo_entry(ofl->ofl_lml, _cnt,
2929 2936 &sip[_cnt], &symtab[_cnt], strtab, dyn));
2930 2937 }
2931 2938 }
2932 2939 return (1);
2933 2940 }
2934 2941
2935 2942 /*
2936 2943 * Build the output elf header.
2937 2944 */
2938 2945 static uintptr_t
2939 2946 update_oehdr(Ofl_desc * ofl)
2940 2947 {
2941 2948 Ehdr *ehdr = ofl->ofl_nehdr;
2942 2949
2943 2950 /*
2944 2951 * If an entry point symbol has already been established (refer
2945 2952 * sym_validate()) simply update the elf header entry point with the
2946 2953 * symbols value. If no entry point is defined it will have been filled
2947 2954 * with the start address of the first section within the text segment
2948 2955 * (refer update_outfile()).
2949 2956 */
2950 2957 if (ofl->ofl_entry)
2951 2958 ehdr->e_entry =
2952 2959 ((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;
2953 2960
2954 2961 ehdr->e_ident[EI_DATA] = ld_targ.t_m.m_data;
2955 2962 ehdr->e_version = ofl->ofl_dehdr->e_version;
2956 2963
2957 2964 /*
2958 2965 * When generating a relocatable object under -z symbolcap, set the
2959 2966 * e_machine to be generic, and remove any e_flags. Input relocatable
2960 2967 * objects may identify alternative e_machine (m.machplus) and e_flags
2961 2968 * values. However, the functions within the created output object
2962 2969 * are selected at runtime using the capabilities mechanism, which
2963 2970 * supersedes the e-machine and e_flags information. Therefore,
2964 2971 * e_machine and e_flag values are not propagated to the output object,
2965 2972 * as these values might prevent the kernel from loading the object
2966 2973 * before the runtime linker gets control.
2967 2974 */
2968 2975 if (ofl->ofl_flags & FLG_OF_OTOSCAP) {
2969 2976 ehdr->e_machine = ld_targ.t_m.m_mach;
2970 2977 ehdr->e_flags = 0;
2971 2978 } else {
2972 2979 /*
2973 2980 * Note. it may be necessary to update the e_flags field in the
2974 2981 * machine dependent section.
2975 2982 */
2976 2983 ehdr->e_machine = ofl->ofl_dehdr->e_machine;
2977 2984 ehdr->e_flags = ofl->ofl_dehdr->e_flags;
2978 2985
2979 2986 if (ehdr->e_machine != ld_targ.t_m.m_mach) {
2980 2987 if (ehdr->e_machine != ld_targ.t_m.m_machplus)
2981 2988 return (S_ERROR);
2982 2989 if ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0)
2983 2990 return (S_ERROR);
2984 2991 }
2985 2992 }
2986 2993
2987 2994 if (ofl->ofl_flags & FLG_OF_SHAROBJ)
2988 2995 ehdr->e_type = ET_DYN;
2989 2996 else if (ofl->ofl_flags & FLG_OF_RELOBJ)
2990 2997 ehdr->e_type = ET_REL;
2991 2998 else
2992 2999 ehdr->e_type = ET_EXEC;
2993 3000
2994 3001 return (1);
2995 3002 }
2996 3003
2997 3004 /*
2998 3005 * Perform move table expansion.
2999 3006 */
3000 3007 static void
3001 3008 expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *mvp)
3002 3009 {
3003 3010 Os_desc *osp;
3004 3011 uchar_t *taddr, *taddr0;
3005 3012 Sxword offset;
3006 3013 Half cnt;
3007 3014 uint_t stride;
3008 3015
3009 3016 osp = ofl->ofl_isparexpn->is_osdesc;
3010 3017 offset = sdp->sd_sym->st_value - osp->os_shdr->sh_addr;
3011 3018
3012 3019 taddr0 = taddr = osp->os_outdata->d_buf;
3013 3020 taddr += offset;
3014 3021 taddr = taddr + mvp->m_poffset;
3015 3022
3016 3023 for (cnt = 0; cnt < mvp->m_repeat; cnt++) {
3017 3024 /* LINTED */
3018 3025 DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mvp,
3019 3026 (Addr)(taddr - taddr0)));
3020 3027 stride = (uint_t)mvp->m_stride + 1;
3021 3028
3022 3029 /*
3023 3030 * Update the target address based upon the move entry size.
3024 3031 * This size was validated in ld_process_move().
3025 3032 */
3026 3033 /* LINTED */
3027 3034 switch (ELF_M_SIZE(mvp->m_info)) {
3028 3035 case 1:
3029 3036 /* LINTED */
3030 3037 *taddr = (uchar_t)mvp->m_value;
3031 3038 taddr += stride;
3032 3039 break;
3033 3040 case 2:
3034 3041 /* LINTED */
3035 3042 *((Half *)taddr) = (Half)mvp->m_value;
3036 3043 taddr += 2 * stride;
3037 3044 break;
3038 3045 case 4:
3039 3046 /* LINTED */
3040 3047 *((Word *)taddr) = (Word)mvp->m_value;
3041 3048 taddr += 4 * stride;
3042 3049 break;
3043 3050 case 8:
3044 3051 /* LINTED */
3045 3052 *((u_longlong_t *)taddr) = mvp->m_value;
3046 3053 taddr += 8 * stride;
3047 3054 break;
3048 3055 }
3049 3056 }
3050 3057 }
3051 3058
3052 3059 /*
3053 3060 * Update Move sections.
3054 3061 */
3055 3062 static void
3056 3063 update_move(Ofl_desc *ofl)
3057 3064 {
3058 3065 Word ndx = 0;
3059 3066 ofl_flag_t flags = ofl->ofl_flags;
3060 3067 Move *omvp;
3061 3068 Aliste idx1;
3062 3069 Sym_desc *sdp;
3063 3070
3064 3071 /*
3065 3072 * Determine the index of the symbol table that will be referenced by
3066 3073 * the Move section.
3067 3074 */
3068 3075 if (OFL_ALLOW_DYNSYM(ofl))
3069 3076 /* LINTED */
3070 3077 ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
3071 3078 else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
3072 3079 /* LINTED */
3073 3080 ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3074 3081
3075 3082 /*
3076 3083 * Update sh_link of the Move section, and point to the new Move data.
3077 3084 */
3078 3085 if (ofl->ofl_osmove) {
3079 3086 ofl->ofl_osmove->os_shdr->sh_link = ndx;
3080 3087 omvp = (Move *)ofl->ofl_osmove->os_outdata->d_buf;
3081 3088 }
3082 3089
3083 3090 /*
3084 3091 * Update symbol entry index
3085 3092 */
3086 3093 for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx1, sdp)) {
3087 3094 Aliste idx2;
3088 3095 Mv_desc *mdp;
3089 3096
3090 3097 /*
3091 3098 * Expand move table
3092 3099 */
3093 3100 if (sdp->sd_flags & FLG_SY_PAREXPN) {
3094 3101 const char *str;
3095 3102
3096 3103 if (flags & FLG_OF_STATIC)
3097 3104 str = MSG_INTL(MSG_PSYM_EXPREASON1);
3098 3105 else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
3099 3106 str = MSG_INTL(MSG_PSYM_EXPREASON2);
3100 3107 else
3101 3108 str = MSG_INTL(MSG_PSYM_EXPREASON3);
3102 3109
3103 3110 DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
3104 3111 sdp->sd_name, str));
3105 3112
3106 3113 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
3107 3114 DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
3108 3115 mdp->md_move, sdp));
3109 3116 expand_move(ofl, sdp, mdp->md_move);
3110 3117 }
3111 3118 continue;
3112 3119 }
3113 3120
3114 3121 /*
3115 3122 * Process move table
3116 3123 */
3117 3124 DBG_CALL(Dbg_move_outmove(ofl->ofl_lml, sdp->sd_name));
3118 3125
3119 3126 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
3120 3127 Move *imvp;
3121 3128 int idx = 1;
3122 3129 Sym *sym;
3123 3130
3124 3131 imvp = mdp->md_move;
3125 3132 sym = sdp->sd_sym;
3126 3133
3127 3134 DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, imvp, sdp));
3128 3135
3129 3136 *omvp = *imvp;
3130 3137 if ((flags & FLG_OF_RELOBJ) == 0) {
3131 3138 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
3132 3139 Os_desc *osp = sdp->sd_isc->is_osdesc;
3133 3140 Word ndx = osp->os_identndx;
3134 3141
3135 3142 omvp->m_info =
3136 3143 /* LINTED */
3137 3144 ELF_M_INFO(ndx, imvp->m_info);
3138 3145
3139 3146 if (ELF_ST_TYPE(sym->st_info) !=
3140 3147 STT_SECTION) {
3141 3148 omvp->m_poffset =
3142 3149 sym->st_value -
3143 3150 osp->os_shdr->sh_addr +
3144 3151 imvp->m_poffset;
3145 3152 }
3146 3153 } else {
3147 3154 omvp->m_info =
3148 3155 /* LINTED */
3149 3156 ELF_M_INFO(sdp->sd_symndx,
3150 3157 imvp->m_info);
3151 3158 }
3152 3159 } else {
3153 3160 Boolean isredloc = FALSE;
3154 3161
3155 3162 if ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) &&
3156 3163 (ofl->ofl_flags & FLG_OF_REDLSYM))
3157 3164 isredloc = TRUE;
3158 3165
3159 3166 if (isredloc && !(sdp->sd_move)) {
3160 3167 Os_desc *osp = sdp->sd_isc->is_osdesc;
3161 3168 Word ndx = osp->os_identndx;
3162 3169
3163 3170 omvp->m_info =
3164 3171 /* LINTED */
3165 3172 ELF_M_INFO(ndx, imvp->m_info);
3166 3173
3167 3174 omvp->m_poffset += sym->st_value;
3168 3175 } else {
3169 3176 if (isredloc)
3170 3177 DBG_CALL(Dbg_syms_reduce(ofl,
3171 3178 DBG_SYM_REDUCE_RETAIN,
3172 3179 sdp, idx,
3173 3180 ofl->ofl_osmove->os_name));
3174 3181
3175 3182 omvp->m_info =
3176 3183 /* LINTED */
3177 3184 ELF_M_INFO(sdp->sd_symndx,
3178 3185 imvp->m_info);
3179 3186 }
3180 3187 }
3181 3188
3182 3189 DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, omvp, sdp));
3183 3190 omvp++;
3184 3191 idx++;
3185 3192 }
3186 3193 }
3187 3194 }
3188 3195
3189 3196 /*
3190 3197 * Scan through the SHT_GROUP output sections. Update their sh_link/sh_info
3191 3198 * fields as well as the section contents.
3192 3199 */
3193 3200 static uintptr_t
3194 3201 update_ogroup(Ofl_desc *ofl)
3195 3202 {
3196 3203 Aliste idx;
3197 3204 Os_desc *osp;
3198 3205 uintptr_t error = 0;
3199 3206
3200 3207 for (APLIST_TRAVERSE(ofl->ofl_osgroups, idx, osp)) {
3201 3208 Is_desc *isp;
3202 3209 Ifl_desc *ifl;
3203 3210 Shdr *shdr = osp->os_shdr;
3204 3211 Sym_desc *sdp;
3205 3212 Xword i, grpcnt;
3206 3213 Word *gdata;
3207 3214
3208 3215 /*
3209 3216 * Since input GROUP sections always create unique
3210 3217 * output GROUP sections - we know there is only one
3211 3218 * item on the list.
3212 3219 */
3213 3220 isp = ld_os_first_isdesc(osp);
3214 3221
3215 3222 ifl = isp->is_file;
3216 3223 sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
3217 3224 shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3218 3225 shdr->sh_info = sdp->sd_symndx;
3219 3226
3220 3227 /*
3221 3228 * Scan through the group data section and update
3222 3229 * all of the links to new values.
3223 3230 */
3224 3231 grpcnt = shdr->sh_size / shdr->sh_entsize;
3225 3232 gdata = (Word *)osp->os_outdata->d_buf;
3226 3233
3227 3234 for (i = 1; i < grpcnt; i++) {
3228 3235 Os_desc *_osp;
3229 3236 Is_desc *_isp = ifl->ifl_isdesc[gdata[i]];
3230 3237
3231 3238 /*
3232 3239 * If the referenced section didn't make it to the
3233 3240 * output file - just zero out the entry.
3234 3241 */
3235 3242 if ((_osp = _isp->is_osdesc) == NULL)
3236 3243 gdata[i] = 0;
3237 3244 else
3238 3245 gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
3239 3246 }
3240 3247 }
3241 3248 return (error);
3242 3249 }
3243 3250
3244 3251 static void
3245 3252 update_ostrtab(Os_desc *osp, Str_tbl *stp, uint_t extra)
3246 3253 {
3247 3254 Elf_Data *data;
3248 3255
3249 3256 if (osp == NULL)
3250 3257 return;
3251 3258
3252 3259 data = osp->os_outdata;
3253 3260 assert(data->d_size == (st_getstrtab_sz(stp) + extra));
3254 3261 (void) st_setstrbuf(stp, data->d_buf, data->d_size - extra);
3255 3262 /* If leaving an extra hole at the end, zero it */
3256 3263 if (extra > 0)
3257 3264 (void) memset((char *)data->d_buf + data->d_size - extra,
3258 3265 0x0, extra);
3259 3266 }
3260 3267
3261 3268 /*
3262 3269 * Update capabilities information.
3263 3270 *
3264 3271 * If string table capabilities exist, then the associated string must be
3265 3272 * translated into an offset into the string table.
3266 3273 */
3267 3274 static void
3268 3275 update_oscap(Ofl_desc *ofl)
3269 3276 {
3270 3277 Os_desc *strosp, *cosp;
3271 3278 Cap *cap;
3272 3279 Str_tbl *strtbl;
3273 3280 Capstr *capstr;
3274 3281 size_t stoff;
3275 3282 Aliste idx1;
3276 3283
3277 3284 /*
3278 3285 * Determine which symbol table or string table is appropriate.
3279 3286 */
3280 3287 if (OFL_IS_STATIC_OBJ(ofl)) {
3281 3288 strosp = ofl->ofl_osstrtab;
3282 3289 strtbl = ofl->ofl_strtab;
3283 3290 } else {
3284 3291 strosp = ofl->ofl_osdynstr;
3285 3292 strtbl = ofl->ofl_dynstrtab;
3286 3293 }
3287 3294
3288 3295 /*
3289 3296 * If symbol capabilities exist, set the sh_link field of the .SUNW_cap
3290 3297 * section to the .SUNW_capinfo section.
3291 3298 */
3292 3299 if (ofl->ofl_oscapinfo) {
3293 3300 cosp = ofl->ofl_oscap;
3294 3301 cosp->os_shdr->sh_link =
3295 3302 (Word)elf_ndxscn(ofl->ofl_oscapinfo->os_scn);
3296 3303 }
3297 3304
3298 3305 /*
3299 3306 * If there are capability strings to process, set the sh_info
3300 3307 * field of the .SUNW_cap section to the associated string table, and
3301 3308 * proceed to process any CA_SUNW_PLAT entries.
3302 3309 */
3303 3310 if ((ofl->ofl_flags & FLG_OF_CAPSTRS) == 0)
3304 3311 return;
3305 3312
3306 3313 cosp = ofl->ofl_oscap;
3307 3314 cosp->os_shdr->sh_info = (Word)elf_ndxscn(strosp->os_scn);
3308 3315
3309 3316 cap = ofl->ofl_oscap->os_outdata->d_buf;
3310 3317
3311 3318 /*
3312 3319 * Determine whether an object capability identifier, or object
3313 3320 * machine/platform capabilities exists.
3314 3321 */
3315 3322 capstr = &ofl->ofl_ocapset.oc_id;
3316 3323 if (capstr->cs_str) {
3317 3324 (void) st_setstring(strtbl, capstr->cs_str, &stoff);
3318 3325 cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3319 3326 }
3320 3327 for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_plat.cl_val, idx1, capstr)) {
3321 3328 (void) st_setstring(strtbl, capstr->cs_str, &stoff);
3322 3329 cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3323 3330 }
3324 3331 for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_mach.cl_val, idx1, capstr)) {
3325 3332 (void) st_setstring(strtbl, capstr->cs_str, &stoff);
3326 3333 cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3327 3334 }
3328 3335
3329 3336 /*
3330 3337 * Determine any symbol capability identifiers, or machine/platform
3331 3338 * capabilities.
3332 3339 */
3333 3340 if (ofl->ofl_capgroups) {
3334 3341 Cap_group *cgp;
3335 3342
3336 3343 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
3337 3344 Objcapset *ocapset = &cgp->cg_set;
3338 3345 Aliste idx2;
3339 3346
3340 3347 capstr = &ocapset->oc_id;
3341 3348 if (capstr->cs_str) {
3342 3349 (void) st_setstring(strtbl, capstr->cs_str,
3343 3350 &stoff);
3344 3351 cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3345 3352 }
3346 3353 for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx2,
3347 3354 capstr)) {
3348 3355 (void) st_setstring(strtbl, capstr->cs_str,
3349 3356 &stoff);
3350 3357 cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3351 3358 }
3352 3359 for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx2,
3353 3360 capstr)) {
3354 3361 (void) st_setstring(strtbl, capstr->cs_str,
3355 3362 &stoff);
3356 3363 cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3357 3364 }
3358 3365 }
3359 3366 }
3360 3367 }
3361 3368
3362 3369 /*
3363 3370 * Update the .SUNW_capinfo, and possibly the .SUNW_capchain sections.
3364 3371 */
3365 3372 static void
3366 3373 update_oscapinfo(Ofl_desc *ofl)
3367 3374 {
3368 3375 Os_desc *symosp, *ciosp, *ccosp = NULL;
3369 3376 Capinfo *ocapinfo;
3370 3377 Capchain *ocapchain;
3371 3378 Cap_avlnode *cav;
3372 3379 Word chainndx = 0;
3373 3380
3374 3381 /*
3375 3382 * Determine which symbol table is appropriate.
3376 3383 */
3377 3384 if (OFL_IS_STATIC_OBJ(ofl))
3378 3385 symosp = ofl->ofl_ossymtab;
3379 3386 else
3380 3387 symosp = ofl->ofl_osdynsym;
3381 3388
3382 3389 /*
3383 3390 * Update the .SUNW_capinfo sh_link to point to the appropriate symbol
3384 3391 * table section. If we're creating a dynamic object, the
3385 3392 * .SUNW_capinfo sh_info is updated to point to the .SUNW_capchain
3386 3393 * section.
3387 3394 */
3388 3395 ciosp = ofl->ofl_oscapinfo;
3389 3396 ciosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
3390 3397
3391 3398 if (OFL_IS_STATIC_OBJ(ofl) == 0) {
3392 3399 ccosp = ofl->ofl_oscapchain;
3393 3400 ciosp->os_shdr->sh_info = (Word)elf_ndxscn(ccosp->os_scn);
3394 3401 }
3395 3402
3396 3403 /*
3397 3404 * Establish the data for each section. The first element of each
3398 3405 * section defines the section's version number.
3399 3406 */
3400 3407 ocapinfo = ciosp->os_outdata->d_buf;
3401 3408 ocapinfo[0] = CAPINFO_CURRENT;
3402 3409 if (ccosp) {
3403 3410 ocapchain = ccosp->os_outdata->d_buf;
3404 3411 ocapchain[chainndx++] = CAPCHAIN_CURRENT;
3405 3412 }
3406 3413
3407 3414 /*
3408 3415 * Traverse all capabilities families. Each member has a .SUNW_capinfo
3409 3416 * assignment. The .SUNW_capinfo entry differs for relocatable objects
3410 3417 * and dynamic objects.
3411 3418 *
3412 3419 * Relocatable objects:
3413 3420 * ELF_C_GROUP ELF_C_SYM
3414 3421 *
3415 3422 * Family lead: CAPINFO_SUNW_GLOB lead symbol index
3416 3423 * Family lead alias: CAPINFO_SUNW_GLOB lead symbol index
3417 3424 * Family member: .SUNW_cap index lead symbol index
3418 3425 *
3419 3426 * Dynamic objects:
3420 3427 * ELF_C_GROUP ELF_C_SYM
3421 3428 *
3422 3429 * Family lead: CAPINFO_SUNW_GLOB .SUNW_capchain index
3423 3430 * Family lead alias: CAPINFO_SUNW_GLOB .SUNW_capchain index
3424 3431 * Family member: .SUNW_cap index lead symbol index
3425 3432 *
3426 3433 * The ELF_C_GROUP field identifies a capabilities symbol. Lead
3427 3434 * capability symbols, and lead capability aliases are identified by
3428 3435 * a CAPINFO_SUNW_GLOB group identifier. For family members, the
3429 3436 * ELF_C_GROUP provides an index to the associate capabilities group
3430 3437 * (i.e, an index into the SUNW_cap section that defines a group).
3431 3438 *
3432 3439 * For relocatable objects, the ELF_C_SYM field identifies the lead
3433 3440 * capability symbol. For the lead symbol itself, the .SUNW_capinfo
3434 3441 * index is the same as the ELF_C_SYM value. For lead alias symbols,
3435 3442 * the .SUNW_capinfo index differs from the ELF_C_SYM value. This
3436 3443 * differentiation of CAPINFO_SUNW_GLOB symbols allows ld(1) to
3437 3444 * identify, and propagate lead alias symbols. For example, the lead
3438 3445 * capability symbol memcpy() would have the ELF_C_SYM for memcpy(),
3439 3446 * and the lead alias _memcpy() would also have the ELF_C_SYM for
3440 3447 * memcpy().
3441 3448 *
3442 3449 * For dynamic objects, both a lead capability symbol, and alias symbol
3443 3450 * would have a ELF_C_SYM value that represents the same capability
3444 3451 * chain index. The capability chain allows ld.so.1 to traverse a
3445 3452 * family chain for a given lead symbol, and select the most appropriate
3446 3453 * family member. The .SUNW_capchain array contains a series of symbol
3447 3454 * indexes for each family member:
3448 3455 *
3449 3456 * chaincap[n] chaincap[n + 1] chaincap[n + 2] chaincap[n + x]
3450 3457 * foo() ndx foo%x() ndx foo%y() ndx 0
3451 3458 *
3452 3459 * For family members, the ELF_C_SYM value associates the capability
3453 3460 * members with their family lead symbol. This association, although
3454 3461 * unused within a dynamic object, allows ld(1) to identify, and
3455 3462 * propagate family members when processing relocatable objects.
3456 3463 */
3457 3464 for (cav = avl_first(ofl->ofl_capfamilies); cav;
3458 3465 cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
3459 3466 Cap_sym *csp;
3460 3467 Aliste idx;
3461 3468 Sym_desc *asdp, *lsdp = cav->cn_symavlnode.sav_sdp;
3462 3469
3463 3470 if (ccosp) {
3464 3471 /*
3465 3472 * For a dynamic object, identify this lead symbol, and
3466 3473 * point it to the head of a capability chain. Set the
3467 3474 * head of the capability chain to the same lead symbol.
3468 3475 */
3469 3476 ocapinfo[lsdp->sd_symndx] =
3470 3477 ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
3471 3478 ocapchain[chainndx] = lsdp->sd_symndx;
3472 3479 } else {
3473 3480 /*
3474 3481 * For a relocatable object, identify this lead symbol,
3475 3482 * and set the lead symbol index to itself.
3476 3483 */
3477 3484 ocapinfo[lsdp->sd_symndx] =
3478 3485 ELF_C_INFO(lsdp->sd_symndx, CAPINFO_SUNW_GLOB);
3479 3486 }
3480 3487
3481 3488 /*
3482 3489 * Gather any lead symbol aliases.
3483 3490 */
3484 3491 for (APLIST_TRAVERSE(cav->cn_aliases, idx, asdp)) {
3485 3492 if (ccosp) {
3486 3493 /*
3487 3494 * For a dynamic object, identify this lead
3488 3495 * alias symbol, and point it to the same
3489 3496 * capability chain index as the lead symbol.
3490 3497 */
3491 3498 ocapinfo[asdp->sd_symndx] =
3492 3499 ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
3493 3500 } else {
3494 3501 /*
3495 3502 * For a relocatable object, identify this lead
3496 3503 * alias symbol, and set the lead symbol index
3497 3504 * to the lead symbol.
3498 3505 */
3499 3506 ocapinfo[asdp->sd_symndx] =
3500 3507 ELF_C_INFO(lsdp->sd_symndx,
3501 3508 CAPINFO_SUNW_GLOB);
3502 3509 }
3503 3510 }
3504 3511
3505 3512 chainndx++;
3506 3513
3507 3514 /*
3508 3515 * Gather the family members.
3509 3516 */
3510 3517 for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
3511 3518 Sym_desc *msdp = csp->cs_sdp;
3512 3519
3513 3520 /*
3514 3521 * Identify the members capability group, and the lead
3515 3522 * symbol of the family this symbol is a member of.
3516 3523 */
3517 3524 ocapinfo[msdp->sd_symndx] =
3518 3525 ELF_C_INFO(lsdp->sd_symndx, csp->cs_group->cg_ndx);
3519 3526 if (ccosp) {
3520 3527 /*
3521 3528 * For a dynamic object, set the next capability
3522 3529 * chain to point to this family member.
3523 3530 */
3524 3531 ocapchain[chainndx++] = msdp->sd_symndx;
3525 3532 }
3526 3533 }
3527 3534
3528 3535 /*
3529 3536 * Any chain of family members is terminated with a 0 element.
3530 3537 */
3531 3538 if (ccosp)
3532 3539 ocapchain[chainndx++] = 0;
3533 3540 }
3534 3541 }
3535 3542
3536 3543 /*
3537 3544 * Translate the shdr->sh_{link, info} from its input section value to that
3538 3545 * of the corresponding shdr->sh_{link, info} output section value.
3539 3546 */
3540 3547 static Word
3541 3548 translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
3542 3549 {
3543 3550 Is_desc *isp;
3544 3551 Ifl_desc *ifl;
3545 3552
3546 3553 /*
3547 3554 * Don't translate the special section numbers.
3548 3555 */
3549 3556 if (link >= SHN_LORESERVE)
3550 3557 return (link);
3551 3558
3552 3559 /*
3553 3560 * Does this output section translate back to an input file. If not
3554 3561 * then there is no translation to do. In this case we will assume that
3555 3562 * if sh_link has a value, it's the right value.
3556 3563 */
3557 3564 isp = ld_os_first_isdesc(osp);
3558 3565 if ((ifl = isp->is_file) == NULL)
3559 3566 return (link);
3560 3567
3561 3568 /*
3562 3569 * Sanity check to make sure that the sh_{link, info} value
3563 3570 * is within range for the input file.
3564 3571 */
3565 3572 if (link >= ifl->ifl_shnum) {
3566 3573 ld_eprintf(ofl, ERR_WARNING, msg, ifl->ifl_name,
3567 3574 EC_WORD(isp->is_scnndx), isp->is_name, EC_XWORD(link));
3568 3575 return (link);
3569 3576 }
3570 3577
3571 3578 /*
3572 3579 * Follow the link to the input section.
3573 3580 */
3574 3581 if ((isp = ifl->ifl_isdesc[link]) == NULL)
3575 3582 return (0);
3576 3583 if ((osp = isp->is_osdesc) == NULL)
3577 3584 return (0);
3578 3585
3579 3586 /* LINTED */
3580 3587 return ((Word)elf_ndxscn(osp->os_scn));
3581 3588 }
3582 3589
3583 3590 /*
3584 3591 * Having created all of the necessary sections, segments, and associated
3585 3592 * headers, fill in the program headers and update any other data in the
3586 3593 * output image. Some general rules:
3587 3594 *
3588 3595 * - If an interpreter is required always generate a PT_PHDR entry as
3589 3596 * well. It is this entry that triggers the kernel into passing the
3590 3597 * interpreter an aux vector instead of just a file descriptor.
3591 3598 *
3592 3599 * - When generating an image that will be interpreted (ie. a dynamic
3593 3600 * executable, a shared object, or a static executable that has been
3594 3601 * provided with an interpreter - weird, but possible), make the initial
3595 3602 * loadable segment include both the ehdr and phdr[]. Both of these
3596 3603 * tables are used by the interpreter therefore it seems more intuitive
3597 3604 * to explicitly defined them as part of the mapped image rather than
3598 3605 * relying on page rounding by the interpreter to allow their access.
3599 3606 *
3600 3607 * - When generating a static image that does not require an interpreter
3601 3608 * have the first loadable segment indicate the address of the first
3602 3609 * .section as the start address (things like /kernel/unix and ufsboot
3603 3610 * expect this behavior).
3604 3611 */
3605 3612 uintptr_t
3606 3613 ld_update_outfile(Ofl_desc *ofl)
3607 3614 {
3608 3615 Addr size, etext, vaddr;
3609 3616 Sg_desc *sgp;
3610 3617 Sg_desc *dtracesgp = NULL, *capsgp = NULL, *intpsgp = NULL;
3611 3618 Os_desc *osp;
3612 3619 int phdrndx = 0, segndx = -1, secndx, intppndx, intpsndx;
3613 3620 int dtracepndx, dtracesndx, cappndx, capsndx;
3614 3621 Ehdr *ehdr = ofl->ofl_nehdr;
3615 3622 Shdr *hshdr;
3616 3623 Phdr *_phdr = NULL;
3617 3624 Word phdrsz = (ehdr->e_phnum * ehdr->e_phentsize), shscnndx;
3618 3625 ofl_flag_t flags = ofl->ofl_flags;
3619 3626 Word ehdrsz = ehdr->e_ehsize;
3620 3627 Boolean nobits;
3621 3628 Off offset;
3622 3629 Aliste idx1;
3623 3630
3624 3631 /*
3625 3632 * Initialize the starting address for the first segment. Executables
3626 3633 * have different starting addresses depending upon the target ABI,
3627 3634 * where as shared objects have a starting address of 0. If this is
3628 3635 * a 64-bit executable that is being constructed to run in a restricted
3629 3636 * address space, use an alternative origin that will provide more free
3630 3637 * address space for the the eventual process.
3631 3638 */
3632 3639 if (ofl->ofl_flags & FLG_OF_EXEC) {
3633 3640 #if defined(_ELF64)
3634 3641 if (ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_ADDR32)
3635 3642 vaddr = ld_targ.t_m.m_segm_aorigin;
3636 3643 else
3637 3644 #endif
3638 3645 vaddr = ld_targ.t_m.m_segm_origin;
3639 3646 } else
3640 3647 vaddr = 0;
3641 3648
3642 3649 /*
3643 3650 * Loop through the segment descriptors and pick out what we need.
3644 3651 */
3645 3652 DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
3646 3653 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3647 3654 Phdr *phdr = &(sgp->sg_phdr);
3648 3655 Xword p_align;
3649 3656 Aliste idx2;
3650 3657 Sym_desc *sdp;
3651 3658
3652 3659 segndx++;
3653 3660
3654 3661 /*
3655 3662 * If an interpreter is required generate a PT_INTERP and
3656 3663 * PT_PHDR program header entry. The PT_PHDR entry describes
3657 3664 * the program header table itself. This information will be
3658 3665 * passed via the aux vector to the interpreter (ld.so.1).
3659 3666 * The program header array is actually part of the first
3660 3667 * loadable segment (and the PT_PHDR entry is the first entry),
3661 3668 * therefore its virtual address isn't known until the first
3662 3669 * loadable segment is processed.
3663 3670 */
3664 3671 if (phdr->p_type == PT_PHDR) {
3665 3672 if (ofl->ofl_osinterp) {
3666 3673 phdr->p_offset = ehdr->e_phoff;
3667 3674 phdr->p_filesz = phdr->p_memsz = phdrsz;
3668 3675
3669 3676 DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3670 3677 ofl->ofl_phdr[phdrndx++] = *phdr;
3671 3678 }
3672 3679 continue;
3673 3680 }
3674 3681 if (phdr->p_type == PT_INTERP) {
3675 3682 if (ofl->ofl_osinterp) {
3676 3683 intpsgp = sgp;
3677 3684 intpsndx = segndx;
3678 3685 intppndx = phdrndx++;
3679 3686 }
3680 3687 continue;
3681 3688 }
3682 3689
3683 3690 /*
3684 3691 * If we are creating a PT_SUNWDTRACE segment, remember where
3685 3692 * the program header is. The header values are assigned after
3686 3693 * update_osym() has completed and the symbol table addresses
3687 3694 * have been updated.
3688 3695 */
3689 3696 if (phdr->p_type == PT_SUNWDTRACE) {
3690 3697 if (ofl->ofl_dtracesym &&
3691 3698 ((flags & FLG_OF_RELOBJ) == 0)) {
3692 3699 dtracesgp = sgp;
3693 3700 dtracesndx = segndx;
3694 3701 dtracepndx = phdrndx++;
3695 3702 }
3696 3703 continue;
3697 3704 }
3698 3705
3699 3706 /*
3700 3707 * If a hardware/software capabilities section is required,
3701 3708 * generate the PT_SUNWCAP header. Note, as this comes before
3702 3709 * the first loadable segment, we don't yet know its real
3703 3710 * virtual address. This is updated later.
3704 3711 */
3705 3712 if (phdr->p_type == PT_SUNWCAP) {
3706 3713 if (ofl->ofl_oscap && (ofl->ofl_flags & FLG_OF_PTCAP) &&
3707 3714 ((flags & FLG_OF_RELOBJ) == 0)) {
3708 3715 capsgp = sgp;
3709 3716 capsndx = segndx;
3710 3717 cappndx = phdrndx++;
3711 3718 }
3712 3719 continue;
3713 3720 }
3714 3721
3715 3722 /*
3716 3723 * As the dynamic program header occurs after the loadable
3717 3724 * headers in the segment descriptor table, all the address
3718 3725 * information for the .dynamic output section will have been
3719 3726 * figured out by now.
3720 3727 */
3721 3728 if (phdr->p_type == PT_DYNAMIC) {
3722 3729 if (OFL_ALLOW_DYNSYM(ofl)) {
3723 3730 Shdr *shdr = ofl->ofl_osdynamic->os_shdr;
3724 3731
3725 3732 phdr->p_vaddr = shdr->sh_addr;
3726 3733 phdr->p_offset = shdr->sh_offset;
3727 3734 phdr->p_filesz = shdr->sh_size;
3728 3735 phdr->p_flags = ld_targ.t_m.m_dataseg_perm;
3729 3736
3730 3737 DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3731 3738 ofl->ofl_phdr[phdrndx++] = *phdr;
3732 3739 }
3733 3740 continue;
3734 3741 }
3735 3742
3736 3743 /*
3737 3744 * As the unwind (.eh_frame_hdr) program header occurs after
3738 3745 * the loadable headers in the segment descriptor table, all
3739 3746 * the address information for the .eh_frame output section
3740 3747 * will have been figured out by now.
3741 3748 */
3742 3749 if (phdr->p_type == PT_SUNW_UNWIND) {
3743 3750 Shdr *shdr;
3744 3751
3745 3752 if (ofl->ofl_unwindhdr == NULL)
3746 3753 continue;
3747 3754
3748 3755 shdr = ofl->ofl_unwindhdr->os_shdr;
3749 3756
3750 3757 phdr->p_flags = PF_R;
3751 3758 phdr->p_vaddr = shdr->sh_addr;
3752 3759 phdr->p_memsz = shdr->sh_size;
3753 3760 phdr->p_filesz = shdr->sh_size;
3754 3761 phdr->p_offset = shdr->sh_offset;
3755 3762 phdr->p_align = shdr->sh_addralign;
3756 3763 phdr->p_paddr = 0;
3757 3764 ofl->ofl_phdr[phdrndx++] = *phdr;
3758 3765 continue;
3759 3766 }
3760 3767
3761 3768 /*
3762 3769 * The sunwstack program is used to convey non-default
3763 3770 * flags for the process stack. Only emit it if it would
3764 3771 * change the default.
3765 3772 */
3766 3773 if (phdr->p_type == PT_SUNWSTACK) {
3767 3774 if (((flags & FLG_OF_RELOBJ) == 0) &&
3768 3775 ((sgp->sg_flags & FLG_SG_DISABLED) == 0))
3769 3776 ofl->ofl_phdr[phdrndx++] = *phdr;
3770 3777 continue;
3771 3778 }
3772 3779
3773 3780 /*
3774 3781 * As the TLS program header occurs after the loadable
3775 3782 * headers in the segment descriptor table, all the address
3776 3783 * information for the .tls output section will have been
3777 3784 * figured out by now.
3778 3785 */
3779 3786 if (phdr->p_type == PT_TLS) {
3780 3787 Os_desc *tlsosp;
3781 3788 Shdr *lastfileshdr = NULL;
3782 3789 Shdr *firstshdr = NULL, *lastshdr;
3783 3790 Aliste idx;
3784 3791
3785 3792 if (ofl->ofl_ostlsseg == NULL)
3786 3793 continue;
3787 3794
3788 3795 /*
3789 3796 * Scan the output sections that have contributed TLS.
3790 3797 * Remember the first and last so as to determine the
3791 3798 * TLS memory size requirement. Remember the last
3792 3799 * progbits section to determine the TLS data
3793 3800 * contribution, which determines the TLS program
3794 3801 * header filesz.
3795 3802 */
3796 3803 for (APLIST_TRAVERSE(ofl->ofl_ostlsseg, idx, tlsosp)) {
3797 3804 Shdr *tlsshdr = tlsosp->os_shdr;
3798 3805
3799 3806 if (firstshdr == NULL)
3800 3807 firstshdr = tlsshdr;
3801 3808 if (tlsshdr->sh_type != SHT_NOBITS)
3802 3809 lastfileshdr = tlsshdr;
3803 3810 lastshdr = tlsshdr;
3804 3811 }
3805 3812
3806 3813 phdr->p_flags = PF_R | PF_W;
3807 3814 phdr->p_vaddr = firstshdr->sh_addr;
3808 3815 phdr->p_offset = firstshdr->sh_offset;
3809 3816 phdr->p_align = firstshdr->sh_addralign;
3810 3817
3811 3818 /*
3812 3819 * Determine the initialized TLS data size. This
3813 3820 * address range is from the start of the TLS segment
3814 3821 * to the end of the last piece of initialized data.
3815 3822 */
3816 3823 if (lastfileshdr)
3817 3824 phdr->p_filesz = lastfileshdr->sh_offset +
3818 3825 lastfileshdr->sh_size - phdr->p_offset;
3819 3826 else
3820 3827 phdr->p_filesz = 0;
3821 3828
3822 3829 /*
3823 3830 * Determine the total TLS memory size. This includes
3824 3831 * all TLS data and TLS uninitialized data. This
3825 3832 * address range is from the start of the TLS segment
3826 3833 * to the memory address of the last piece of
3827 3834 * uninitialized data.
3828 3835 */
3829 3836 phdr->p_memsz = lastshdr->sh_addr +
3830 3837 lastshdr->sh_size - phdr->p_vaddr;
3831 3838
3832 3839 DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3833 3840 ofl->ofl_phdr[phdrndx] = *phdr;
3834 3841 ofl->ofl_tlsphdr = &ofl->ofl_phdr[phdrndx++];
3835 3842 continue;
3836 3843 }
3837 3844
3838 3845 /*
3839 3846 * If this is an empty segment declaration, it will occur after
3840 3847 * all other loadable segments. As empty segments can be
3841 3848 * defined with fixed addresses, make sure that no loadable
3842 3849 * segments overlap. This might occur as the object evolves
3843 3850 * and the loadable segments grow, thus encroaching upon an
3844 3851 * existing segment reservation.
3845 3852 *
3846 3853 * Segments are only created for dynamic objects, thus this
3847 3854 * checking can be skipped when building a relocatable object.
3848 3855 */
3849 3856 if (!(flags & FLG_OF_RELOBJ) &&
3850 3857 (sgp->sg_flags & FLG_SG_EMPTY)) {
3851 3858 int i;
3852 3859 Addr v_e;
3853 3860
3854 3861 vaddr = phdr->p_vaddr;
3855 3862 phdr->p_memsz = sgp->sg_length;
3856 3863 DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3857 3864 ofl->ofl_phdr[phdrndx++] = *phdr;
3858 3865
3859 3866 if (phdr->p_type != PT_LOAD)
3860 3867 continue;
3861 3868
3862 3869 v_e = vaddr + phdr->p_memsz;
3863 3870
3864 3871 /*
3865 3872 * Check overlaps
3866 3873 */
3867 3874 for (i = 0; i < phdrndx - 1; i++) {
3868 3875 Addr p_s = (ofl->ofl_phdr[i]).p_vaddr;
3869 3876 Addr p_e;
3870 3877
3871 3878 if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
3872 3879 continue;
3873 3880
3874 3881 p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
3875 3882 if (((p_s <= vaddr) && (p_e > vaddr)) ||
3876 3883 ((vaddr <= p_s) && (v_e > p_s)))
3877 3884 ld_eprintf(ofl, ERR_WARNING,
3878 3885 MSG_INTL(MSG_UPD_SEGOVERLAP),
3879 3886 ofl->ofl_name, EC_ADDR(p_e),
3880 3887 sgp->sg_name, EC_ADDR(vaddr));
3881 3888 }
3882 3889 continue;
3883 3890 }
3884 3891
3885 3892 /*
3886 3893 * Having processed any of the special program headers any
3887 3894 * remaining headers will be built to express individual
3888 3895 * segments. Segments are only built if they have output
3889 3896 * section descriptors associated with them (ie. some form of
3890 3897 * input section has been matched to this segment).
3891 3898 */
3892 3899 if (sgp->sg_osdescs == NULL)
3893 3900 continue;
3894 3901
3895 3902 /*
3896 3903 * Determine the segments offset and size from the section
3897 3904 * information provided from elf_update().
3898 3905 * Allow for multiple NOBITS sections.
3899 3906 */
3900 3907 osp = sgp->sg_osdescs->apl_data[0];
3901 3908 hshdr = osp->os_shdr;
3902 3909
3903 3910 phdr->p_filesz = 0;
3904 3911 phdr->p_memsz = 0;
3905 3912 phdr->p_offset = offset = hshdr->sh_offset;
3906 3913
3907 3914 nobits = ((hshdr->sh_type == SHT_NOBITS) &&
3908 3915 ((sgp->sg_flags & FLG_SG_PHREQ) == 0));
3909 3916
3910 3917 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3911 3918 Shdr *shdr = osp->os_shdr;
3912 3919
3913 3920 p_align = 0;
3914 3921 if (shdr->sh_addralign > p_align)
3915 3922 p_align = shdr->sh_addralign;
3916 3923
3917 3924 offset = (Off)S_ROUND(offset, shdr->sh_addralign);
3918 3925 offset += shdr->sh_size;
3919 3926
3920 3927 if (shdr->sh_type != SHT_NOBITS) {
3921 3928 if (nobits) {
3922 3929 ld_eprintf(ofl, ERR_FATAL,
3923 3930 MSG_INTL(MSG_UPD_NOBITS));
3924 3931 return (S_ERROR);
3925 3932 }
3926 3933 phdr->p_filesz = offset - phdr->p_offset;
3927 3934 } else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
3928 3935 nobits = TRUE;
3929 3936 }
3930 3937 phdr->p_memsz = offset - hshdr->sh_offset;
3931 3938
3932 3939 /*
3933 3940 * If this is the first loadable segment of a dynamic object,
3934 3941 * or an interpreter has been specified (a static object built
3935 3942 * with an interpreter will still be given a PT_HDR entry), then
3936 3943 * compensate for the elf header and program header array. Both
3937 3944 * of these are actually part of the loadable segment as they
3938 3945 * may be inspected by the interpreter. Adjust the segments
3939 3946 * size and offset accordingly.
3940 3947 */
3941 3948 if ((_phdr == NULL) && (phdr->p_type == PT_LOAD) &&
3942 3949 ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
3943 3950 (!(ofl->ofl_dtflags_1 & DF_1_NOHDR))) {
3944 3951 size = (Addr)S_ROUND((phdrsz + ehdrsz),
3945 3952 hshdr->sh_addralign);
3946 3953 phdr->p_offset -= size;
3947 3954 phdr->p_filesz += size;
3948 3955 phdr->p_memsz += size;
3949 3956 }
3950 3957
3951 3958 /*
3952 3959 * If segment size symbols are required (specified via a
3953 3960 * mapfile) update their value.
3954 3961 */
3955 3962 for (APLIST_TRAVERSE(sgp->sg_sizesym, idx2, sdp))
3956 3963 sdp->sd_sym->st_value = phdr->p_memsz;
3957 3964
3958 3965 /*
3959 3966 * If no file content has been assigned to this segment (it
3960 3967 * only contains no-bits sections), then reset the offset for
3961 3968 * consistency.
3962 3969 */
3963 3970 if (phdr->p_filesz == 0)
3964 3971 phdr->p_offset = 0;
3965 3972
3966 3973 /*
3967 3974 * If a virtual address has been specified for this segment
3968 3975 * from a mapfile use it and make sure the previous segment
3969 3976 * does not run into this segment.
3970 3977 */
3971 3978 if (phdr->p_type == PT_LOAD) {
3972 3979 if ((sgp->sg_flags & FLG_SG_P_VADDR)) {
3973 3980 if (_phdr && (vaddr > phdr->p_vaddr) &&
3974 3981 (phdr->p_type == PT_LOAD))
3975 3982 ld_eprintf(ofl, ERR_WARNING,
3976 3983 MSG_INTL(MSG_UPD_SEGOVERLAP),
3977 3984 ofl->ofl_name, EC_ADDR(vaddr),
3978 3985 sgp->sg_name,
3979 3986 EC_ADDR(phdr->p_vaddr));
3980 3987 vaddr = phdr->p_vaddr;
3981 3988 phdr->p_align = 0;
3982 3989 } else {
3983 3990 vaddr = phdr->p_vaddr =
3984 3991 (Addr)S_ROUND(vaddr, phdr->p_align);
3985 3992 }
3986 3993 }
3987 3994
3988 3995 /*
3989 3996 * Adjust the address offset and p_align if needed.
3990 3997 */
3991 3998 if (((sgp->sg_flags & FLG_SG_P_VADDR) == 0) &&
3992 3999 ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0)) {
3993 4000 if (phdr->p_align != 0)
3994 4001 vaddr += phdr->p_offset % phdr->p_align;
3995 4002 else
3996 4003 vaddr += phdr->p_offset;
3997 4004 phdr->p_vaddr = vaddr;
3998 4005 }
3999 4006
4000 4007 /*
4001 4008 * If an interpreter is required set the virtual address of the
4002 4009 * PT_PHDR program header now that we know the virtual address
4003 4010 * of the loadable segment that contains it. Update the
4004 4011 * PT_SUNWCAP header similarly.
4005 4012 */
4006 4013 if ((_phdr == NULL) && (phdr->p_type == PT_LOAD)) {
4007 4014 _phdr = phdr;
4008 4015
4009 4016 if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) {
4010 4017 if (ofl->ofl_osinterp)
4011 4018 ofl->ofl_phdr[0].p_vaddr =
4012 4019 vaddr + ehdrsz;
4013 4020
4014 4021 /*
4015 4022 * Finally, if we're creating a dynamic object
4016 4023 * (or a static object in which an interpreter
4017 4024 * is specified) update the vaddr to reflect
4018 4025 * the address of the first section within this
4019 4026 * segment.
4020 4027 */
4021 4028 if ((ofl->ofl_osinterp) ||
4022 4029 (flags & FLG_OF_DYNAMIC))
4023 4030 vaddr += size;
4024 4031 } else {
4025 4032 /*
4026 4033 * If the DF_1_NOHDR flag was set, and an
4027 4034 * interpreter is being generated, the PT_PHDR
4028 4035 * will not be part of any loadable segment.
4029 4036 */
4030 4037 if (ofl->ofl_osinterp) {
4031 4038 ofl->ofl_phdr[0].p_vaddr = 0;
4032 4039 ofl->ofl_phdr[0].p_memsz = 0;
4033 4040 ofl->ofl_phdr[0].p_flags = 0;
4034 4041 }
4035 4042 }
4036 4043 }
4037 4044
4038 4045 /*
4039 4046 * Ensure the ELF entry point defaults to zero. Typically, this
4040 4047 * value is overridden in update_oehdr() to one of the standard
4041 4048 * entry points. Historically, this default was set to the
4042 4049 * address of first executable section, but this has since been
4043 4050 * found to be more confusing than it is helpful.
4044 4051 */
4045 4052 ehdr->e_entry = 0;
4046 4053
4047 4054 DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
4048 4055
4049 4056 /*
4050 4057 * Traverse the output section descriptors for this segment so
4051 4058 * that we can update the section headers addresses. We've
4052 4059 * calculated the virtual address of the initial section within
4053 4060 * this segment, so each successive section can be calculated
4054 4061 * based on their offsets from each other.
4055 4062 */
4056 4063 secndx = 0;
4057 4064 hshdr = 0;
4058 4065 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
4059 4066 Shdr *shdr = osp->os_shdr;
4060 4067
4061 4068 if (shdr->sh_link)
4062 4069 shdr->sh_link = translate_link(ofl, osp,
4063 4070 shdr->sh_link, MSG_INTL(MSG_FIL_INVSHLINK));
4064 4071
4065 4072 if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
4066 4073 shdr->sh_info = translate_link(ofl, osp,
4067 4074 shdr->sh_info, MSG_INTL(MSG_FIL_INVSHINFO));
4068 4075
4069 4076 if (!(flags & FLG_OF_RELOBJ) &&
4070 4077 (phdr->p_type == PT_LOAD)) {
4071 4078 if (hshdr)
4072 4079 vaddr += (shdr->sh_offset -
4073 4080 hshdr->sh_offset);
4074 4081
4075 4082 shdr->sh_addr = vaddr;
4076 4083 hshdr = shdr;
4077 4084 }
4078 4085
4079 4086 DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
4080 4087 secndx++;
4081 4088 }
4082 4089
4083 4090 /*
4084 4091 * Establish the virtual address of the end of the last section
4085 4092 * in this segment so that the next segments offset can be
4086 4093 * calculated from this.
4087 4094 */
4088 4095 if (hshdr)
4089 4096 vaddr += hshdr->sh_size;
4090 4097
4091 4098 /*
4092 4099 * Output sections for this segment complete. Adjust the
4093 4100 * virtual offset for the last sections size, and make sure we
4094 4101 * haven't exceeded any maximum segment length specification.
4095 4102 */
4096 4103 if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
4097 4104 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_UPD_LARGSIZE),
4098 4105 ofl->ofl_name, sgp->sg_name,
4099 4106 EC_XWORD(phdr->p_memsz), EC_XWORD(sgp->sg_length));
4100 4107 return (S_ERROR);
4101 4108 }
4102 4109
4103 4110 if (phdr->p_type == PT_NOTE) {
4104 4111 phdr->p_vaddr = 0;
4105 4112 phdr->p_paddr = 0;
4106 4113 phdr->p_align = 0;
4107 4114 phdr->p_memsz = 0;
4108 4115 }
4109 4116
4110 4117 if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
4111 4118 ofl->ofl_phdr[phdrndx++] = *phdr;
4112 4119 }
4113 4120
4114 4121 /*
4115 4122 * Update any new output sections. When building the initial output
4116 4123 * image, a number of sections were created but left uninitialized (eg.
4117 4124 * .dynsym, .dynstr, .symtab, .symtab, etc.). Here we update these
4118 4125 * sections with the appropriate data. Other sections may still be
4119 4126 * modified via reloc_process().
4120 4127 *
4121 4128 * Copy the interpreter name into the .interp section.
4122 4129 */
4123 4130 if (ofl->ofl_interp)
4124 4131 (void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
4125 4132 ofl->ofl_interp);
4126 4133
4127 4134 /*
4128 4135 * Update the .shstrtab, .strtab and .dynstr sections.
4129 4136 */
4130 4137 update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab, 0);
4131 4138 update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab, 0);
4132 4139 update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab, DYNSTR_EXTRA_PAD);
4133 4140
4134 4141 /*
4135 4142 * Build any output symbol tables, the symbols information is copied
4136 4143 * and updated into the new output image.
4137 4144 */
4138 4145 if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
4139 4146 return (S_ERROR);
4140 4147
4141 4148 /*
4142 4149 * If we have an PT_INTERP phdr, update it now from the associated
4143 4150 * section information.
4144 4151 */
4145 4152 if (intpsgp) {
4146 4153 Phdr *phdr = &(intpsgp->sg_phdr);
4147 4154 Shdr *shdr = ofl->ofl_osinterp->os_shdr;
4148 4155
4149 4156 phdr->p_vaddr = shdr->sh_addr;
4150 4157 phdr->p_offset = shdr->sh_offset;
4151 4158 phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
4152 4159 phdr->p_flags = PF_R;
4153 4160
4154 4161 DBG_CALL(Dbg_seg_entry(ofl, intpsndx, intpsgp));
4155 4162 ofl->ofl_phdr[intppndx] = *phdr;
4156 4163 }
4157 4164
4158 4165 /*
4159 4166 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
4160 4167 * the symbol. It's only now been updated via update_sym().
4161 4168 */
4162 4169 if (dtracesgp) {
4163 4170 Phdr *aphdr, *phdr = &(dtracesgp->sg_phdr);
4164 4171 Sym_desc *sdp = ofl->ofl_dtracesym;
4165 4172
4166 4173 phdr->p_vaddr = sdp->sd_sym->st_value;
4167 4174 phdr->p_memsz = sdp->sd_sym->st_size;
4168 4175
4169 4176 /*
4170 4177 * Take permissions from the segment that the symbol is
4171 4178 * associated with.
4172 4179 */
4173 4180 aphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
4174 4181 assert(aphdr);
4175 4182 phdr->p_flags = aphdr->p_flags;
4176 4183
4177 4184 DBG_CALL(Dbg_seg_entry(ofl, dtracesndx, dtracesgp));
4178 4185 ofl->ofl_phdr[dtracepndx] = *phdr;
4179 4186 }
4180 4187
4181 4188 /*
4182 4189 * If we have a PT_SUNWCAP phdr, update it now from the associated
4183 4190 * section information.
4184 4191 */
4185 4192 if (capsgp) {
4186 4193 Phdr *phdr = &(capsgp->sg_phdr);
4187 4194 Shdr *shdr = ofl->ofl_oscap->os_shdr;
4188 4195
4189 4196 phdr->p_vaddr = shdr->sh_addr;
4190 4197 phdr->p_offset = shdr->sh_offset;
4191 4198 phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
4192 4199 phdr->p_flags = PF_R;
4193 4200
4194 4201 DBG_CALL(Dbg_seg_entry(ofl, capsndx, capsgp));
4195 4202 ofl->ofl_phdr[cappndx] = *phdr;
4196 4203 }
4197 4204
4198 4205 /*
4199 4206 * Update the GROUP sections.
4200 4207 */
4201 4208 if (update_ogroup(ofl) == S_ERROR)
4202 4209 return (S_ERROR);
4203 4210
4204 4211 /*
4205 4212 * Update Move Table.
4206 4213 */
4207 4214 if (ofl->ofl_osmove || ofl->ofl_isparexpn)
4208 4215 update_move(ofl);
4209 4216
4210 4217 /*
4211 4218 * Build any output headers, version information, dynamic structure and
4212 4219 * syminfo structure.
4213 4220 */
4214 4221 if (update_oehdr(ofl) == S_ERROR)
4215 4222 return (S_ERROR);
4216 4223 if (!(flags & FLG_OF_NOVERSEC)) {
4217 4224 if ((flags & FLG_OF_VERDEF) &&
4218 4225 (update_overdef(ofl) == S_ERROR))
4219 4226 return (S_ERROR);
4220 4227 if ((flags & FLG_OF_VERNEED) &&
4221 4228 (update_overneed(ofl) == S_ERROR))
4222 4229 return (S_ERROR);
4223 4230 if (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))
4224 4231 update_oversym(ofl);
4225 4232 }
4226 4233 if (flags & FLG_OF_DYNAMIC) {
4227 4234 if (update_odynamic(ofl) == S_ERROR)
4228 4235 return (S_ERROR);
4229 4236 }
4230 4237 if (ofl->ofl_ossyminfo) {
4231 4238 if (update_osyminfo(ofl) == S_ERROR)
4232 4239 return (S_ERROR);
4233 4240 }
4234 4241
4235 4242 /*
4236 4243 * Update capabilities information if required.
4237 4244 */
4238 4245 if (ofl->ofl_oscap)
4239 4246 update_oscap(ofl);
4240 4247 if (ofl->ofl_oscapinfo)
4241 4248 update_oscapinfo(ofl);
4242 4249
4243 4250 /*
4244 4251 * Sanity test: the first and last data byte of a string table
4245 4252 * must be NULL.
4246 4253 */
4247 4254 assert((ofl->ofl_osshstrtab == NULL) ||
4248 4255 (*((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) == '\0'));
4249 4256 assert((ofl->ofl_osshstrtab == NULL) ||
4250 4257 (*(((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) +
4251 4258 ofl->ofl_osshstrtab->os_outdata->d_size - 1) == '\0'));
4252 4259
4253 4260 assert((ofl->ofl_osstrtab == NULL) ||
4254 4261 (*((char *)ofl->ofl_osstrtab->os_outdata->d_buf) == '\0'));
4255 4262 assert((ofl->ofl_osstrtab == NULL) ||
4256 4263 (*(((char *)ofl->ofl_osstrtab->os_outdata->d_buf) +
4257 4264 ofl->ofl_osstrtab->os_outdata->d_size - 1) == '\0'));
4258 4265
4259 4266 assert((ofl->ofl_osdynstr == NULL) ||
4260 4267 (*((char *)ofl->ofl_osdynstr->os_outdata->d_buf) == '\0'));
4261 4268 assert((ofl->ofl_osdynstr == NULL) ||
4262 4269 (*(((char *)ofl->ofl_osdynstr->os_outdata->d_buf) +
4263 4270 ofl->ofl_osdynstr->os_outdata->d_size - DYNSTR_EXTRA_PAD - 1) ==
4264 4271 '\0'));
4265 4272
4266 4273 /*
4267 4274 * Emit Strtab diagnostics.
4268 4275 */
4269 4276 DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
4270 4277 ofl->ofl_shdrsttab));
4271 4278 DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
4272 4279 ofl->ofl_strtab));
4273 4280 DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
4274 4281 ofl->ofl_dynstrtab));
4275 4282
4276 4283 /*
4277 4284 * Initialize the section headers string table index within the elf
4278 4285 * header.
4279 4286 */
4280 4287 /* LINTED */
4281 4288 if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
4282 4289 SHN_LORESERVE) {
4283 4290 ofl->ofl_nehdr->e_shstrndx =
4284 4291 /* LINTED */
4285 4292 (Half)shscnndx;
4286 4293 } else {
4287 4294 /*
4288 4295 * If the STRTAB section index doesn't fit into
4289 4296 * e_shstrndx, then we store it in 'shdr[0].st_link'.
4290 4297 */
4291 4298 Elf_Scn *scn;
4292 4299 Shdr *shdr0;
4293 4300
4294 4301 if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
4295 4302 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
4296 4303 ofl->ofl_name);
4297 4304 return (S_ERROR);
4298 4305 }
4299 4306 if ((shdr0 = elf_getshdr(scn)) == NULL) {
4300 4307 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
4301 4308 ofl->ofl_name);
4302 4309 return (S_ERROR);
4303 4310 }
4304 4311 ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
4305 4312 shdr0->sh_link = shscnndx;
4306 4313 }
4307 4314
4308 4315 return ((uintptr_t)etext);
4309 4316 }
↓ open down ↓ |
1765 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX