Print this page
3616 SHF_GROUP sections should not be discarded via other COMDAT mechanisms
3709 need sloppy relocation for GNU .debug_macro
Reviewed by: Joshua M. Clulow <josh@sysmgr.org>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/libld/common/sections.c
+++ new/usr/src/cmd/sgs/libld/common/sections.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 * Module sections. Initialize special sections
31 31 */
32 32
33 33 #define ELF_TARGET_AMD64
34 34
35 35 #include <string.h>
36 36 #include <strings.h>
37 37 #include <stdio.h>
38 38 #include <link.h>
39 39 #include <debug.h>
40 40 #include "msg.h"
41 41 #include "_libld.h"
42 42
43 43 inline static void
44 44 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
45 45 {
46 46 Sym *sym = sdp->sd_sym;
47 47 uchar_t type = ELF_ST_TYPE(sym->st_info);
48 48 /* LINTED - only used for assert() */
49 49 int err;
50 50
51 51 if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) {
52 52 ofl->ofl_locscnt--;
53 53
54 54 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
55 55 assert(err != -1);
56 56
57 57 if (allow_ldynsym && ldynsym_symtype[type]) {
58 58 ofl->ofl_dynlocscnt--;
59 59
60 60 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
61 61 assert(err != -1);
62 62 /* Remove from sort section? */
63 63 DYNSORT_COUNT(sdp, sym, type, --);
64 64 }
65 65 }
66 66 sdp->sd_flags |= FLG_SY_ISDISC;
67 67 }
68 68
69 69 inline static void
70 70 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
71 71 {
72 72 Sym *sym = sdp->sd_sym;
73 73 uchar_t type = ELF_ST_TYPE(sym->st_info);
74 74 /* LINTED - only used for assert() */
75 75 int err;
76 76
77 77 ofl->ofl_scopecnt--;
78 78 ofl->ofl_elimcnt++;
79 79
80 80 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
81 81 assert(err != -1);
82 82
83 83 if (allow_ldynsym && ldynsym_symtype[type]) {
84 84 ofl->ofl_dynscopecnt--;
85 85
86 86 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
87 87 assert(err != -1);
88 88 /* Remove from sort section? */
89 89 DYNSORT_COUNT(sdp, sym, type, --);
90 90 }
91 91 sdp->sd_flags |= FLG_SY_ELIM;
92 92 }
93 93
94 94 inline static void
95 95 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym)
96 96 {
97 97 Os_desc *osp;
98 98 Is_desc *isp = sdp->sd_isc;
99 99 uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info);
100 100
101 101 if (bind == STB_LOCAL) {
102 102 uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info);
103 103
104 104 /*
105 105 * Skip section symbols, these were never collected in the
106 106 * first place.
107 107 */
108 108 if (type == STT_SECTION)
109 109 return;
110 110
111 111 /*
112 112 * Determine if the whole file is being removed. Remove any
113 113 * file symbol, and any symbol that is not associated with a
114 114 * section, provided the symbol has not been identified as
115 115 * (update) required.
116 116 */
117 117 if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
118 118 ((type == STT_FILE) || ((isp == NULL) &&
119 119 ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
120 120 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
121 121 if (ifl->ifl_flags & FLG_IF_IGNORE)
122 122 remove_local(ofl, sdp, allow_ldynsym);
123 123 return;
124 124 }
125 125
126 126 } else {
127 127 /*
128 128 * Global symbols can only be eliminated when the interfaces of
129 129 * an object have been defined via versioning/scoping.
130 130 */
131 131 if (!SYM_IS_HIDDEN(sdp))
132 132 return;
133 133
134 134 /*
135 135 * Remove any unreferenced symbols that are not associated with
136 136 * a section.
137 137 */
138 138 if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) {
139 139 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
140 140 if (ifl->ifl_flags & FLG_IF_IGNORE)
141 141 remove_scoped(ofl, sdp, allow_ldynsym);
142 142 return;
143 143 }
144 144 }
145 145
146 146 /*
147 147 * Do not discard any symbols that are associated with non-allocable
148 148 * segments.
149 149 */
150 150 if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
151 151 ((osp = isp->is_osdesc) != 0) &&
152 152 (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
153 153 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
154 154 if (ifl->ifl_flags & FLG_IF_IGNORE) {
155 155 if (bind == STB_LOCAL)
156 156 remove_local(ofl, sdp, allow_ldynsym);
157 157 else
158 158 remove_scoped(ofl, sdp, allow_ldynsym);
159 159 }
160 160 }
161 161 }
162 162
163 163 /*
164 164 * There are situations where we may count output sections (ofl_shdrcnt)
165 165 * that are subsequently eliminated from the output object. Whether or
166 166 * not this happens cannot be known until all input has been seen and
167 167 * section elimination code has run. However, the situations where this
168 168 * outcome is possible are known, and are flagged by setting FLG_OF_ADJOSCNT.
169 169 *
170 170 * If FLG_OF_ADJOSCNT is set, this routine makes a pass over the output
171 171 * sections. If an unused output section is encountered, we decrement
172 172 * ofl->ofl_shdrcnt and remove the section name from the .shstrtab string
173 173 * table (ofl->ofl_shdrsttab).
174 174 *
175 175 * This code must be kept in sync with the similar code
176 176 * found in outfile.c:ld_create_outfile().
177 177 */
178 178 static void
179 179 adjust_os_count(Ofl_desc *ofl)
180 180 {
181 181 Sg_desc *sgp;
182 182 Is_desc *isp;
183 183 Os_desc *osp;
184 184 Ifl_desc *ifl;
185 185 Aliste idx1;
186 186
187 187 if ((ofl->ofl_flags & FLG_OF_ADJOSCNT) == 0)
188 188 return;
189 189
190 190 /*
191 191 * For each output section, look at the input sections to find at least
192 192 * one input section that has not been eliminated. If none are found,
193 193 * the -z ignore processing above has eliminated that output section.
194 194 */
195 195 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
196 196 Aliste idx2;
197 197 Word ptype = sgp->sg_phdr.p_type;
198 198
199 199 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
200 200 Aliste idx3;
201 201 int keep = 0, os_isdescs_idx;
202 202
203 203 OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
204 204 ifl = isp->is_file;
205 205
206 206 /* Input section is tagged for discard? */
207 207 if (isp->is_flags & FLG_IS_DISCARD)
208 208 continue;
209 209
210 210 /*
211 211 * If the file is discarded, it will take
212 212 * the section with it.
213 213 */
214 214 if (ifl &&
215 215 (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
216 216 ((ptype == PT_LOAD) &&
217 217 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
218 218 (isp->is_shdr->sh_size > 0))) &&
219 219 (ifl->ifl_flags & FLG_IF_IGNORE))
220 220 continue;
221 221
222 222 /*
223 223 * We have found a kept input section,
224 224 * so the output section will be created.
225 225 */
226 226 keep = 1;
227 227 break;
228 228 }
229 229 /*
230 230 * If no section of this name was kept, decrement
231 231 * the count and remove the name from .shstrtab.
232 232 */
233 233 if (keep == 0) {
234 234 /* LINTED - only used for assert() */
235 235 int err;
236 236
237 237 ofl->ofl_shdrcnt--;
238 238 err = st_delstring(ofl->ofl_shdrsttab,
239 239 osp->os_name);
240 240 assert(err != -1);
241 241 }
242 242 }
243 243 }
244 244 }
245 245
246 246 /*
247 247 * If -zignore has been in effect, scan all input files to determine if the
248 248 * file, or sections from the file, have been referenced. If not, the file or
249 249 * some of the files sections can be discarded. If sections are to be
250 250 * discarded, rescan the output relocations and the symbol table and remove
251 251 * the relocations and symbol entries that are no longer required.
252 252 *
253 253 * Note: It's possible that a section which is being discarded has contributed
254 254 * to the GOT table or the PLT table. However, we can't at this point
255 255 * eliminate the corresponding entries. This is because there could well
256 256 * be other sections referencing those same entries, but we don't have
257 257 * the infrastructure to determine this. So, keep the PLT and GOT
258 258 * entries in the table in case someone wants them.
259 259 * Note: The section to be affected needs to be allocatable.
260 260 * So even if -zignore is in effect, if the section is not allocatable,
261 261 * we do not eliminate it.
262 262 */
263 263 static uintptr_t
264 264 ignore_section_processing(Ofl_desc *ofl)
265 265 {
266 266 Sg_desc *sgp;
267 267 Is_desc *isp;
268 268 Os_desc *osp;
269 269 Ifl_desc *ifl;
270 270 Rel_cachebuf *rcbp;
271 271 Rel_desc *rsp;
272 272 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
273 273 Aliste idx1;
274 274
275 275 for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
276 276 uint_t num, discard;
277 277
278 278 /*
279 279 * Diagnose (-D unused) a completely unreferenced file.
280 280 */
281 281 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
282 282 DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
283 283 ifl->ifl_name, 0, 0));
284 284 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
285 285 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
286 286 continue;
287 287
288 288 /*
289 289 * Before scanning the whole symbol table to determine if
290 290 * symbols should be discard - quickly (relatively) scan the
291 291 * sections to determine if any are to be discarded.
292 292 */
293 293 discard = 0;
294 294 if (ifl->ifl_flags & FLG_IF_FILEREF) {
295 295 for (num = 1; num < ifl->ifl_shnum; num++) {
296 296 if (((isp = ifl->ifl_isdesc[num]) != NULL) &&
297 297 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
298 298 ((osp = isp->is_osdesc) != NULL) &&
299 299 ((sgp = osp->os_sgdesc) != NULL) &&
300 300 (sgp->sg_phdr.p_type == PT_LOAD)) {
301 301 discard++;
302 302 break;
303 303 }
304 304 }
305 305 }
306 306
307 307 /*
308 308 * No sections are to be 'ignored'
309 309 */
310 310 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
311 311 continue;
312 312
313 313 /*
314 314 * We know that we have discarded sections. Scan the symbol
315 315 * table for this file to determine if symbols need to be
316 316 * discarded that are associated with the 'ignored' sections.
317 317 */
318 318 for (num = 1; num < ifl->ifl_symscnt; num++) {
319 319 Sym_desc *sdp;
320 320
321 321 /*
322 322 * If the symbol definition has been resolved to another
323 323 * file, or the symbol has already been discarded or
324 324 * eliminated, skip it.
325 325 */
326 326 sdp = ifl->ifl_oldndx[num];
327 327 if ((sdp->sd_file != ifl) ||
328 328 (sdp->sd_flags &
329 329 (FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_ELIM)))
330 330 continue;
331 331
332 332 /*
333 333 * Complete the investigation of the symbol.
334 334 */
335 335 ignore_sym(ofl, ifl, sdp, allow_ldynsym);
336 336 }
337 337 }
338 338
339 339 /*
340 340 * If we were only here to solicit debugging diagnostics, we're done.
341 341 */
342 342 if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
343 343 return (1);
344 344
345 345 /*
346 346 * Scan all output relocations searching for those against discarded or
347 347 * ignored sections. If one is found, decrement the total outrel count.
348 348 */
349 349 REL_CACHE_TRAVERSE(&ofl->ofl_outrels, idx1, rcbp, rsp) {
350 350 Is_desc *isc = rsp->rel_isdesc;
351 351 uint_t flags, entsize;
352 352 Shdr *shdr;
353 353
354 354 if ((isc == NULL) || ((isc->is_flags & (FLG_IS_SECTREF))) ||
355 355 ((ifl = isc->is_file) == NULL) ||
356 356 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
357 357 ((shdr = isc->is_shdr) == NULL) ||
358 358 ((shdr->sh_flags & SHF_ALLOC) == 0))
359 359 continue;
360 360
361 361 flags = rsp->rel_flags;
362 362
363 363 if (flags & (FLG_REL_GOT | FLG_REL_BSS |
364 364 FLG_REL_NOINFO | FLG_REL_PLT))
365 365 continue;
366 366
367 367 osp = RELAUX_GET_OSDESC(rsp);
368 368
369 369 if (rsp->rel_flags & FLG_REL_RELA)
370 370 entsize = sizeof (Rela);
371 371 else
372 372 entsize = sizeof (Rel);
373 373
374 374 assert(osp->os_szoutrels > 0);
375 375 osp->os_szoutrels -= entsize;
376 376
377 377 if (!(flags & FLG_REL_PLT))
378 378 ofl->ofl_reloccntsub++;
379 379
380 380 if (rsp->rel_rtype == ld_targ.t_m.m_r_relative)
381 381 ofl->ofl_relocrelcnt--;
382 382 }
383 383
384 384 /*
385 385 * As a result of our work here, the number of output sections may
386 386 * have decreased. Trigger a call to adjust_os_count().
387 387 */
388 388 ofl->ofl_flags |= FLG_OF_ADJOSCNT;
389 389
390 390 return (1);
391 391 }
392 392
393 393 /*
394 394 * Allocate Elf_Data, Shdr, and Is_desc structures for a new
395 395 * section.
396 396 *
397 397 * entry:
398 398 * ofl - Output file descriptor
399 399 * shtype - SHT_ type code for section.
400 400 * shname - String giving the name for the new section.
401 401 * entcnt - # of items contained in the data part of the new section.
402 402 * This value is multiplied against the known element size
403 403 * for the section type to determine the size of the data
404 404 * area for the section. It is only meaningful in cases where
405 405 * the section type has a non-zero element size. In other cases,
406 406 * the caller must set the size fields in the *ret_data and
407 407 * *ret_shdr structs manually.
408 408 * ret_isec, ret_shdr, ret_data - Address of pointers to
409 409 * receive address of newly allocated structs.
410 410 *
411 411 * exit:
412 412 * On error, returns S_ERROR. On success, returns (1), and the
413 413 * ret_ pointers have been updated to point at the new structures,
414 414 * which have been filled in. To finish the task, the caller must
415 415 * update any fields within the supplied descriptors that differ
416 416 * from its needs, and then call ld_place_section().
417 417 */
418 418 static uintptr_t
419 419 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
420 420 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
421 421 {
422 422 typedef struct sec_info {
423 423 Word d_type;
424 424 Word align; /* Used in both data and section header */
425 425 Word sh_flags;
426 426 Word sh_entsize;
427 427 } SEC_INFO_T;
428 428
429 429 const SEC_INFO_T *sec_info;
430 430
431 431 Shdr *shdr;
432 432 Elf_Data *data;
433 433 Is_desc *isec;
434 434 size_t size;
435 435
436 436 /*
437 437 * For each type of section, we have a distinct set of
438 438 * SEC_INFO_T values. This macro defines a static structure
439 439 * containing those values and generates code to set the sec_info
440 440 * pointer to refer to it. The pointer in sec_info remains valid
441 441 * outside of the declaration scope because the info_s struct is static.
442 442 *
443 443 * We can't determine the value of M_WORD_ALIGN at compile time, so
444 444 * a different variant is used for those cases.
445 445 */
446 446 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
447 447 { \
448 448 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
449 449 sh_entsize}; \
450 450 sec_info = &info_s; \
451 451 }
452 452 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
453 453 { \
454 454 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
455 455 sh_entsize}; \
456 456 info_s.align = ld_targ.t_m.m_word_align; \
457 457 sec_info = &info_s; \
458 458 }
459 459
460 460 switch (shtype) {
461 461 case SHT_PROGBITS:
462 462 /*
463 463 * SHT_PROGBITS sections contain are used for many
464 464 * different sections. Alignments and flags differ.
465 465 * Some have a standard entsize, and others don't.
466 466 * We set some defaults here, but there is no expectation
467 467 * that they are correct or complete for any specific
468 468 * purpose. The caller must provide the correct values.
469 469 */
470 470 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0)
471 471 break;
472 472
473 473 case SHT_SYMTAB:
474 474 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym))
475 475 break;
476 476
477 477 case SHT_DYNSYM:
478 478 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
479 479 break;
480 480
481 481 case SHT_SUNW_LDYNSYM:
482 482 ofl->ofl_flags |= FLG_OF_OSABI;
483 483 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
484 484 break;
485 485
486 486 case SHT_STRTAB:
487 487 /*
488 488 * A string table may or may not be allocable, depending
489 489 * on context, so we leave that flag unset and leave it to
490 490 * the caller to add it if necessary.
491 491 *
492 492 * String tables do not have a standard entsize, so
493 493 * we set it to 0.
494 494 */
495 495 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
496 496 break;
497 497
498 498 case SHT_RELA:
499 499 /*
500 500 * Relocations with an addend (Everything except 32-bit X86).
501 501 * The caller is expected to set all section header flags.
502 502 */
503 503 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela))
504 504 break;
505 505
506 506 case SHT_REL:
507 507 /*
508 508 * Relocations without an addend (32-bit X86 only).
509 509 * The caller is expected to set all section header flags.
510 510 */
511 511 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel))
512 512 break;
513 513
514 514 case SHT_HASH:
515 515 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
516 516 break;
517 517
518 518 case SHT_SUNW_symsort:
519 519 case SHT_SUNW_tlssort:
520 520 ofl->ofl_flags |= FLG_OF_OSABI;
521 521 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
522 522 break;
523 523
524 524 case SHT_DYNAMIC:
525 525 /*
526 526 * A dynamic section may or may not be allocable, and may or
527 527 * may not be writable, depending on context, so we leave the
528 528 * flags unset and leave it to the caller to add them if
529 529 * necessary.
530 530 */
531 531 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, 0, sizeof (Dyn))
532 532 break;
533 533
534 534 case SHT_NOBITS:
535 535 /*
536 536 * SHT_NOBITS is used for BSS-type sections. The size and
537 537 * alignment depend on the specific use and must be adjusted
538 538 * by the caller.
539 539 */
540 540 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
541 541 break;
542 542
543 543 case SHT_INIT_ARRAY:
544 544 case SHT_FINI_ARRAY:
545 545 case SHT_PREINIT_ARRAY:
546 546 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
547 547 sizeof (Addr))
548 548 break;
549 549
550 550 case SHT_SYMTAB_SHNDX:
551 551 /*
552 552 * Note that these sections are created to be associated
553 553 * with both symtab and dynsym symbol tables. However, they
554 554 * are non-allocable in all cases, because the runtime
555 555 * linker has no need for this information. It is purely
556 556 * informational, used by elfdump(1), debuggers, etc.
557 557 */
558 558 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word));
559 559 break;
560 560
561 561 case SHT_SUNW_cap:
562 562 ofl->ofl_flags |= FLG_OF_OSABI;
563 563 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap));
564 564 break;
565 565
566 566 case SHT_SUNW_capchain:
567 567 ofl->ofl_flags |= FLG_OF_OSABI;
568 568 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC,
569 569 sizeof (Capchain));
570 570 break;
571 571
572 572 case SHT_SUNW_capinfo:
573 573 ofl->ofl_flags |= FLG_OF_OSABI;
574 574 #if _ELF64
575 575 SET_SEC_INFO(ELF_T_XWORD, sizeof (Xword), SHF_ALLOC,
576 576 sizeof (Capinfo));
577 577 #else
578 578 SET_SEC_INFO(ELF_T_WORD, sizeof (Word), SHF_ALLOC,
579 579 sizeof (Capinfo));
580 580 #endif
581 581 break;
582 582
583 583 case SHT_SUNW_move:
584 584 ofl->ofl_flags |= FLG_OF_OSABI;
585 585 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
586 586 SHF_ALLOC | SHF_WRITE, sizeof (Move));
587 587 break;
588 588
589 589 case SHT_SUNW_syminfo:
590 590 ofl->ofl_flags |= FLG_OF_OSABI;
591 591 /*
592 592 * The sh_info field of the SHT_*_syminfo section points
593 593 * to the header index of the associated .dynamic section,
594 594 * so we also set SHF_INFO_LINK.
595 595 */
596 596 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE,
597 597 SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
598 598 break;
599 599
600 600 case SHT_SUNW_verneed:
601 601 case SHT_SUNW_verdef:
602 602 ofl->ofl_flags |= FLG_OF_OSABI;
603 603 /*
604 604 * The info for verneed and versym happen to be the same.
605 605 * The entries in these sections are not of uniform size,
606 606 * so we set the entsize to 0.
607 607 */
608 608 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0);
609 609 break;
610 610
611 611 case SHT_SUNW_versym:
612 612 ofl->ofl_flags |= FLG_OF_OSABI;
613 613 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC,
614 614 sizeof (Versym));
615 615 break;
616 616
617 617 default:
618 618 /* Should not happen: fcn called with unknown section type */
619 619 assert(0);
620 620 return (S_ERROR);
621 621 }
622 622 #undef SET_SEC_INFO
623 623 #undef SET_SEC_INFO_WORD_ALIGN
624 624
625 625 size = entcnt * sec_info->sh_entsize;
626 626
627 627 /*
628 628 * Allocate and initialize the Elf_Data structure.
629 629 */
630 630 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
631 631 return (S_ERROR);
632 632 data->d_type = sec_info->d_type;
633 633 data->d_size = size;
634 634 data->d_align = sec_info->align;
635 635 data->d_version = ofl->ofl_dehdr->e_version;
636 636
637 637 /*
638 638 * Allocate and initialize the Shdr structure.
639 639 */
640 640 if ((shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
641 641 return (S_ERROR);
642 642 shdr->sh_type = shtype;
643 643 shdr->sh_size = size;
644 644 shdr->sh_flags = sec_info->sh_flags;
645 645 shdr->sh_addralign = sec_info->align;
646 646 shdr->sh_entsize = sec_info->sh_entsize;
647 647
648 648 /*
649 649 * Allocate and initialize the Is_desc structure.
650 650 */
651 651 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
652 652 return (S_ERROR);
653 653 isec->is_name = shname;
654 654 isec->is_shdr = shdr;
655 655 isec->is_indata = data;
656 656
657 657
658 658 *ret_isec = isec;
659 659 *ret_shdr = shdr;
660 660 *ret_data = data;
661 661 return (1);
662 662 }
663 663
664 664 /*
665 665 * Use an existing input section as a template to create a new
666 666 * input section with the same values as the original, other than
667 667 * the size of the data area which is supplied by the caller.
668 668 *
669 669 * entry:
670 670 * ofl - Output file descriptor
671 671 * ifl - Input file section to use as a template
672 672 * size - Size of data area for new section
673 673 * ret_isec, ret_shdr, ret_data - Address of pointers to
674 674 * receive address of newly allocated structs.
675 675 *
676 676 * exit:
677 677 * On error, returns S_ERROR. On success, returns (1), and the
678 678 * ret_ pointers have been updated to point at the new structures,
679 679 * which have been filled in. To finish the task, the caller must
680 680 * update any fields within the supplied descriptors that differ
681 681 * from its needs, and then call ld_place_section().
682 682 */
683 683 static uintptr_t
684 684 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size,
685 685 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
686 686 {
687 687 Shdr *shdr;
688 688 Elf_Data *data;
689 689 Is_desc *isec;
690 690
691 691 /*
692 692 * Allocate and initialize the Elf_Data structure.
693 693 */
694 694 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
695 695 return (S_ERROR);
696 696 data->d_type = tmpl_isp->is_indata->d_type;
697 697 data->d_size = size;
698 698 data->d_align = tmpl_isp->is_shdr->sh_addralign;
699 699 data->d_version = ofl->ofl_dehdr->e_version;
700 700
701 701 /*
702 702 * Allocate and initialize the Shdr structure.
703 703 */
704 704 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
705 705 return (S_ERROR);
706 706 *shdr = *tmpl_isp->is_shdr;
707 707 shdr->sh_addr = 0;
708 708 shdr->sh_offset = 0;
709 709 shdr->sh_size = size;
710 710
711 711 /*
712 712 * Allocate and initialize the Is_desc structure.
713 713 */
714 714 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
715 715 return (S_ERROR);
716 716 isec->is_name = tmpl_isp->is_name;
717 717 isec->is_shdr = shdr;
718 718 isec->is_indata = data;
719 719
720 720
721 721 *ret_isec = isec;
722 722 *ret_shdr = shdr;
723 723 *ret_data = data;
724 724 return (1);
725 725 }
726 726
727 727 /*
728 728 * Build a .bss section for allocation of tentative definitions. Any `static'
729 729 * .bss definitions would have been associated to their own .bss sections and
730 730 * thus collected from the input files. `global' .bss definitions are tagged
731 731 * as COMMON and do not cause any associated .bss section elements to be
732 732 * generated. Here we add up all these COMMON symbols and generate the .bss
733 733 * section required to represent them.
734 734 */
735 735 uintptr_t
736 736 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t ident)
737 737 {
738 738 Shdr *shdr;
739 739 Elf_Data *data;
740 740 Is_desc *isec;
741 741 Os_desc *osp;
742 742 Xword rsize = (Xword)ofl->ofl_relocbsssz;
743 743
744 744 /*
745 745 * Allocate header structs. We will set the name ourselves below,
746 746 * and there is no entcnt for a BSS. So, the shname and entcnt
747 747 * arguments are 0.
748 748 */
749 749 if (new_section(ofl, SHT_NOBITS, NULL, 0,
750 750 &isec, &shdr, &data) == S_ERROR)
751 751 return (S_ERROR);
752 752
753 753 data->d_size = (size_t)size;
754 754 data->d_align = (size_t)align;
755 755
756 756 shdr->sh_size = size;
757 757 shdr->sh_addralign = align;
758 758
759 759 if (ident == ld_targ.t_id.id_tlsbss) {
760 760 isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
761 761 ofl->ofl_istlsbss = isec;
762 762 shdr->sh_flags |= SHF_TLS;
763 763
764 764 } else if (ident == ld_targ.t_id.id_bss) {
765 765 isec->is_name = MSG_ORIG(MSG_SCN_BSS);
766 766 ofl->ofl_isbss = isec;
767 767
768 768 #if defined(_ELF64)
769 769 } else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
770 770 (ident == ld_targ.t_id.id_lbss)) {
771 771 isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
772 772 ofl->ofl_islbss = isec;
773 773 shdr->sh_flags |= SHF_AMD64_LARGE;
774 774 #endif
775 775 }
776 776
777 777 /*
778 778 * Retain this .*bss input section as this will be where global symbol
779 779 * references are added.
780 780 */
781 781 if ((osp = ld_place_section(ofl, isec, NULL, ident, NULL)) ==
782 782 (Os_desc *)S_ERROR)
783 783 return (S_ERROR);
784 784
785 785 /*
786 786 * If relocations exist against a .*bss section, a section symbol must
787 787 * be created for the section in the .dynsym symbol table.
788 788 */
789 789 if (!(osp->os_flags & FLG_OS_OUTREL)) {
790 790 ofl_flag_t flagtotest;
791 791
792 792 if (ident == ld_targ.t_id.id_tlsbss)
793 793 flagtotest = FLG_OF1_TLSOREL;
794 794 else
795 795 flagtotest = FLG_OF1_BSSOREL;
796 796
797 797 if (ofl->ofl_flags1 & flagtotest) {
798 798 ofl->ofl_dynshdrcnt++;
799 799 osp->os_flags |= FLG_OS_OUTREL;
800 800 }
801 801 }
802 802
803 803 osp->os_szoutrels = rsize;
804 804 return (1);
805 805 }
806 806
807 807 /*
808 808 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
809 809 * ld -z *array=name).
810 810 */
811 811 static uintptr_t
812 812 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp)
813 813 {
814 814 uint_t entcount;
815 815 Aliste idx;
816 816 Elf_Data *data;
817 817 Is_desc *isec;
818 818 Shdr *shdr;
819 819 Sym_desc *sdp;
820 820 Rel_desc reld;
821 821 Rela reloc;
822 822 Os_desc *osp;
823 823 uintptr_t ret = 1;
824 824
825 825 if (alp == NULL)
826 826 return (1);
827 827
828 828 entcount = 0;
829 829 for (APLIST_TRAVERSE(alp, idx, sdp))
830 830 entcount++;
831 831
832 832 if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
833 833 S_ERROR)
834 834 return (S_ERROR);
835 835
836 836 if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == NULL)
837 837 return (S_ERROR);
838 838
839 839 if (ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_array, NULL) ==
840 840 (Os_desc *)S_ERROR)
841 841 return (S_ERROR);
842 842
843 843 osp = isec->is_osdesc;
844 844
845 845 if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY))
846 846 ofl->ofl_osinitarray = osp;
847 847 if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY))
848 848 ofl->ofl_ospreinitarray = osp;
849 849 else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY))
850 850 ofl->ofl_osfiniarray = osp;
851 851
852 852 /*
853 853 * Create relocations against this section to initialize it to the
854 854 * function addresses.
855 855 */
856 856 reld.rel_isdesc = isec;
857 857 reld.rel_aux = NULL;
858 858 reld.rel_flags = FLG_REL_LOAD;
859 859
860 860 /*
861 861 * Fabricate the relocation information (as if a relocation record had
862 862 * been input - see init_rel()).
863 863 */
864 864 reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
865 865 reld.rel_roffset = 0;
866 866 reld.rel_raddend = 0;
867 867
868 868 /*
869 869 * Create a minimal relocation record to satisfy process_sym_reloc()
870 870 * debugging requirements.
871 871 */
872 872 reloc.r_offset = 0;
873 873 reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
874 874 reloc.r_addend = 0;
875 875
876 876 DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
877 877 ld_targ.t_m.m_rel_sht_type));
878 878 for (APLIST_TRAVERSE(alp, idx, sdp)) {
879 879 reld.rel_sym = sdp;
880 880
881 881 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
882 882 MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) {
883 883 ret = S_ERROR;
884 884 continue;
885 885 }
886 886
887 887 reld.rel_roffset += (Xword)sizeof (Addr);
888 888 reloc.r_offset = reld.rel_roffset;
889 889 }
890 890
891 891 return (ret);
892 892 }
893 893
894 894 /*
895 895 * Build a comment section (-Qy option).
896 896 */
897 897 static uintptr_t
898 898 make_comment(Ofl_desc *ofl)
899 899 {
900 900 Shdr *shdr;
901 901 Elf_Data *data;
902 902 Is_desc *isec;
903 903
904 904 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
905 905 &isec, &shdr, &data) == S_ERROR)
906 906 return (S_ERROR);
907 907
908 908 data->d_buf = (void *)ofl->ofl_sgsid;
909 909 data->d_size = strlen(ofl->ofl_sgsid) + 1;
910 910 data->d_align = 1;
911 911
912 912 shdr->sh_size = (Xword)data->d_size;
913 913 shdr->sh_flags = 0;
914 914 shdr->sh_addralign = 1;
915 915
916 916 return ((uintptr_t)ld_place_section(ofl, isec, NULL,
917 917 ld_targ.t_id.id_note, NULL));
918 918 }
919 919
920 920 /*
921 921 * Make the dynamic section. Calculate the size of any strings referenced
922 922 * within this structure, they will be added to the global string table
923 923 * (.dynstr). This routine should be called before make_dynstr().
924 924 *
925 925 * This routine must be maintained in parallel with update_odynamic()
926 926 * in update.c
927 927 */
928 928 static uintptr_t
929 929 make_dynamic(Ofl_desc *ofl)
930 930 {
931 931 Shdr *shdr;
932 932 Os_desc *osp;
933 933 Elf_Data *data;
934 934 Is_desc *isec;
935 935 size_t cnt = 0;
936 936 Aliste idx;
937 937 Ifl_desc *ifl;
938 938 Sym_desc *sdp;
939 939 size_t size;
940 940 Str_tbl *strtbl;
941 941 ofl_flag_t flags = ofl->ofl_flags;
942 942 int not_relobj = !(flags & FLG_OF_RELOBJ);
943 943 int unused = 0;
944 944
945 945 /*
946 946 * Select the required string table.
947 947 */
948 948 if (OFL_IS_STATIC_OBJ(ofl))
949 949 strtbl = ofl->ofl_strtab;
950 950 else
951 951 strtbl = ofl->ofl_dynstrtab;
952 952
953 953 /*
954 954 * Only a limited subset of DT_ entries apply to relocatable
955 955 * objects. See the comment at the head of update_odynamic() in
956 956 * update.c for details.
957 957 */
958 958 if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
959 959 &isec, &shdr, &data) == S_ERROR)
960 960 return (S_ERROR);
961 961
962 962 /*
963 963 * new_section() does not set SHF_ALLOC. If we're building anything
964 964 * besides a relocatable object, then the .dynamic section should
965 965 * reside in allocatable memory.
966 966 */
967 967 if (not_relobj)
968 968 shdr->sh_flags |= SHF_ALLOC;
969 969
970 970 /*
971 971 * new_section() does not set SHF_WRITE. If we're building an object
972 972 * that specifies an interpretor, then a DT_DEBUG entry is created,
973 973 * which is initialized to the applications link-map list at runtime.
974 974 */
975 975 if (ofl->ofl_osinterp)
976 976 shdr->sh_flags |= SHF_WRITE;
977 977
978 978 osp = ofl->ofl_osdynamic =
979 979 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynamic, NULL);
980 980
981 981 /*
982 982 * Reserve entries for any needed dependencies.
983 983 */
984 984 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
985 985 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
986 986 continue;
987 987
988 988 /*
989 989 * If this dependency didn't satisfy any symbol references,
990 990 * generate a debugging diagnostic (ld(1) -Dunused can be used
991 991 * to display these). If this is a standard needed dependency,
992 992 * and -z ignore is in effect, drop the dependency. Explicitly
993 993 * defined dependencies (i.e., -N dep) don't get dropped, and
994 994 * are flagged as being required to simplify update_odynamic()
995 995 * processing.
996 996 */
997 997 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
998 998 ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
999 999 if (unused++ == 0)
1000 1000 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1001 1001 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
1002 1002 (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
1003 1003
1004 1004 /*
1005 1005 * Guidance: Remove unused dependency.
1006 1006 *
1007 1007 * If -z ignore is in effect, this warning is not
1008 1008 * needed because we will quietly remove the unused
1009 1009 * dependency.
1010 1010 */
1011 1011 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_UNUSED) &&
1012 1012 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
1013 1013 ld_eprintf(ofl, ERR_GUIDANCE,
1014 1014 MSG_INTL(MSG_GUIDE_UNUSED),
1015 1015 ifl->ifl_soname);
1016 1016
1017 1017 if (ifl->ifl_flags & FLG_IF_NEEDSTR)
1018 1018 ifl->ifl_flags |= FLG_IF_DEPREQD;
1019 1019 else if (ifl->ifl_flags & FLG_IF_IGNORE)
1020 1020 continue;
1021 1021 }
1022 1022
1023 1023 /*
1024 1024 * If this object requires a DT_POSFLAG_1 entry, reserve it.
1025 1025 */
1026 1026 if ((ifl->ifl_flags & MSK_IF_POSFLAG1) && not_relobj)
1027 1027 cnt++;
1028 1028
1029 1029 if (st_insert(strtbl, ifl->ifl_soname) == -1)
1030 1030 return (S_ERROR);
1031 1031 cnt++;
1032 1032
1033 1033 /*
1034 1034 * If the needed entry contains the $ORIGIN token make sure
1035 1035 * the associated DT_1_FLAGS entry is created.
1036 1036 */
1037 1037 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
1038 1038 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1039 1039 ofl->ofl_dtflags |= DF_ORIGIN;
1040 1040 }
1041 1041 }
1042 1042
1043 1043 if (unused)
1044 1044 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1045 1045
1046 1046 if (not_relobj) {
1047 1047 /*
1048 1048 * Reserve entries for any per-symbol auxiliary/filter strings.
1049 1049 */
1050 1050 cnt += alist_nitems(ofl->ofl_dtsfltrs);
1051 1051
1052 1052 /*
1053 1053 * Reserve entries for _init() and _fini() section addresses.
1054 1054 */
1055 1055 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1056 1056 SYM_NOHASH, NULL, ofl)) != NULL) &&
1057 1057 (sdp->sd_ref == REF_REL_NEED) &&
1058 1058 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1059 1059 sdp->sd_flags |= FLG_SY_UPREQD;
1060 1060 cnt++;
1061 1061 }
1062 1062 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1063 1063 SYM_NOHASH, NULL, ofl)) != NULL) &&
1064 1064 (sdp->sd_ref == REF_REL_NEED) &&
1065 1065 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1066 1066 sdp->sd_flags |= FLG_SY_UPREQD;
1067 1067 cnt++;
1068 1068 }
1069 1069
1070 1070 /*
1071 1071 * Reserve entries for any soname, filter name (shared libs
1072 1072 * only), run-path pointers, cache names and audit requirements.
1073 1073 */
1074 1074 if (ofl->ofl_soname) {
1075 1075 cnt++;
1076 1076 if (st_insert(strtbl, ofl->ofl_soname) == -1)
1077 1077 return (S_ERROR);
1078 1078 }
1079 1079 if (ofl->ofl_filtees) {
1080 1080 cnt++;
1081 1081 if (st_insert(strtbl, ofl->ofl_filtees) == -1)
1082 1082 return (S_ERROR);
1083 1083
1084 1084 /*
1085 1085 * If the filtees entry contains the $ORIGIN token
1086 1086 * make sure the associated DT_1_FLAGS entry is created.
1087 1087 */
1088 1088 if (strstr(ofl->ofl_filtees,
1089 1089 MSG_ORIG(MSG_STR_ORIGIN))) {
1090 1090 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1091 1091 ofl->ofl_dtflags |= DF_ORIGIN;
1092 1092 }
1093 1093 }
1094 1094 }
1095 1095
1096 1096 if (ofl->ofl_rpath) {
1097 1097 cnt += 2; /* DT_RPATH & DT_RUNPATH */
1098 1098 if (st_insert(strtbl, ofl->ofl_rpath) == -1)
1099 1099 return (S_ERROR);
1100 1100
1101 1101 /*
1102 1102 * If the rpath entry contains the $ORIGIN token make sure
1103 1103 * the associated DT_1_FLAGS entry is created.
1104 1104 */
1105 1105 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
1106 1106 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1107 1107 ofl->ofl_dtflags |= DF_ORIGIN;
1108 1108 }
1109 1109 }
1110 1110
1111 1111 if (not_relobj) {
1112 1112 Aliste idx;
1113 1113 Sg_desc *sgp;
1114 1114
1115 1115 if (ofl->ofl_config) {
1116 1116 cnt++;
1117 1117 if (st_insert(strtbl, ofl->ofl_config) == -1)
1118 1118 return (S_ERROR);
1119 1119
1120 1120 /*
1121 1121 * If the config entry contains the $ORIGIN token
1122 1122 * make sure the associated DT_1_FLAGS entry is created.
1123 1123 */
1124 1124 if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1125 1125 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1126 1126 ofl->ofl_dtflags |= DF_ORIGIN;
1127 1127 }
1128 1128 }
1129 1129 if (ofl->ofl_depaudit) {
1130 1130 cnt++;
1131 1131 if (st_insert(strtbl, ofl->ofl_depaudit) == -1)
1132 1132 return (S_ERROR);
1133 1133 }
1134 1134 if (ofl->ofl_audit) {
1135 1135 cnt++;
1136 1136 if (st_insert(strtbl, ofl->ofl_audit) == -1)
1137 1137 return (S_ERROR);
1138 1138 }
1139 1139
1140 1140 /*
1141 1141 * Reserve entries for the DT_HASH, DT_STRTAB, DT_STRSZ,
1142 1142 * DT_SYMTAB, DT_SYMENT, and DT_CHECKSUM.
1143 1143 */
1144 1144 cnt += 6;
1145 1145
1146 1146 /*
1147 1147 * If we are including local functions at the head of
1148 1148 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1149 1149 * and DT_SUNW_SYMSZ.
1150 1150 */
1151 1151 if (OFL_ALLOW_LDYNSYM(ofl))
1152 1152 cnt += 2;
1153 1153
1154 1154 if ((ofl->ofl_dynsymsortcnt > 0) ||
1155 1155 (ofl->ofl_dyntlssortcnt > 0))
1156 1156 cnt++; /* DT_SUNW_SORTENT */
1157 1157
1158 1158 if (ofl->ofl_dynsymsortcnt > 0)
1159 1159 cnt += 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1160 1160
1161 1161 if (ofl->ofl_dyntlssortcnt > 0)
1162 1162 cnt += 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1163 1163
1164 1164 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1165 1165 FLG_OF_VERDEF)
1166 1166 cnt += 2; /* DT_VERDEF & DT_VERDEFNUM */
1167 1167
1168 1168 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1169 1169 FLG_OF_VERNEED)
1170 1170 cnt += 2; /* DT_VERNEED & DT_VERNEEDNUM */
1171 1171
1172 1172 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1173 1173 cnt++; /* DT_RELACOUNT */
1174 1174
1175 1175 if (flags & FLG_OF_TEXTREL) /* DT_TEXTREL */
1176 1176 cnt++;
1177 1177
1178 1178 if (ofl->ofl_osfiniarray) /* DT_FINI_ARRAY */
1179 1179 cnt += 2; /* DT_FINI_ARRAYSZ */
1180 1180
1181 1181 if (ofl->ofl_osinitarray) /* DT_INIT_ARRAY */
1182 1182 cnt += 2; /* DT_INIT_ARRAYSZ */
1183 1183
1184 1184 if (ofl->ofl_ospreinitarray) /* DT_PREINIT_ARRAY & */
1185 1185 cnt += 2; /* DT_PREINIT_ARRAYSZ */
1186 1186
1187 1187 /*
1188 1188 * If we have plt's reserve a DT_PLTRELSZ, DT_PLTREL and
1189 1189 * DT_JMPREL.
1190 1190 */
1191 1191 if (ofl->ofl_pltcnt)
1192 1192 cnt += 3;
1193 1193
1194 1194 /*
1195 1195 * If plt padding is needed (Sparcv9).
1196 1196 */
1197 1197 if (ofl->ofl_pltpad)
1198 1198 cnt += 2; /* DT_PLTPAD & DT_PLTPADSZ */
1199 1199
1200 1200 /*
1201 1201 * If we have any relocations reserve a DT_REL, DT_RELSZ and
1202 1202 * DT_RELENT entry.
1203 1203 */
1204 1204 if (ofl->ofl_relocsz)
1205 1205 cnt += 3;
1206 1206
1207 1207 /*
1208 1208 * If a syminfo section is required create DT_SYMINFO,
1209 1209 * DT_SYMINSZ, and DT_SYMINENT entries.
1210 1210 */
1211 1211 if (flags & FLG_OF_SYMINFO)
1212 1212 cnt += 3;
1213 1213
1214 1214 /*
1215 1215 * If there are any partially initialized sections allocate
1216 1216 * DT_MOVETAB, DT_MOVESZ and DT_MOVEENT.
1217 1217 */
1218 1218 if (ofl->ofl_osmove)
1219 1219 cnt += 3;
1220 1220
1221 1221 /*
1222 1222 * Allocate one DT_REGISTER entry for every register symbol.
1223 1223 */
1224 1224 cnt += ofl->ofl_regsymcnt;
1225 1225
1226 1226 /*
1227 1227 * Reserve a entry for each '-zrtldinfo=...' specified
1228 1228 * on the command line.
1229 1229 */
1230 1230 for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp))
1231 1231 cnt++;
1232 1232
1233 1233 /*
1234 1234 * The following entry should only be placed in a segment that
1235 1235 * is writable.
1236 1236 */
1237 1237 if (((sgp = osp->os_sgdesc) != NULL) &&
1238 1238 (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp)
1239 1239 cnt++; /* DT_DEBUG */
1240 1240
1241 1241 /*
1242 1242 * Capabilities require a .dynamic entry for the .SUNW_cap
1243 1243 * section.
1244 1244 */
1245 1245 if (ofl->ofl_oscap)
1246 1246 cnt++; /* DT_SUNW_CAP */
1247 1247
1248 1248 /*
1249 1249 * Symbol capabilities require a .dynamic entry for the
1250 1250 * .SUNW_capinfo section.
1251 1251 */
1252 1252 if (ofl->ofl_oscapinfo)
1253 1253 cnt++; /* DT_SUNW_CAPINFO */
1254 1254
1255 1255 /*
1256 1256 * Capabilities chain information requires a .SUNW_capchain
1257 1257 * entry (DT_SUNW_CAPCHAIN), entry size (DT_SUNW_CAPCHAINENT),
1258 1258 * and total size (DT_SUNW_CAPCHAINSZ).
1259 1259 */
1260 1260 if (ofl->ofl_oscapchain)
1261 1261 cnt += 3;
1262 1262
1263 1263 if (flags & FLG_OF_SYMBOLIC)
1264 1264 cnt++; /* DT_SYMBOLIC */
1265 1265 }
1266 1266
1267 1267 /*
1268 1268 * Account for Architecture dependent .dynamic entries, and defaults.
1269 1269 */
1270 1270 (*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1271 1271
1272 1272 /*
1273 1273 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1274 1274 * allow room for the unused extra DT_NULLs. These are included
1275 1275 * to allow an ELF editor room to add items later.
1276 1276 */
1277 1277 cnt += 4 + DYNAMIC_EXTRA_ELTS;
1278 1278
1279 1279 /*
1280 1280 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1281 1281 * linker that produced the output object. This information
1282 1282 * allows us to determine whether a given object was linked
1283 1283 * natively, or by a linker running on a different type of
1284 1284 * system. This information can be valuable if one suspects
1285 1285 * that a problem might be due to alignment or byte order issues.
1286 1286 */
1287 1287 cnt++;
1288 1288
1289 1289 /*
1290 1290 * Determine the size of the section from the number of entries.
1291 1291 */
1292 1292 size = cnt * (size_t)shdr->sh_entsize;
1293 1293
1294 1294 shdr->sh_size = (Xword)size;
1295 1295 data->d_size = size;
1296 1296
1297 1297 /*
1298 1298 * There are several tags that are specific to the Solaris osabi
1299 1299 * range which we unconditionally put into any dynamic section
1300 1300 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1301 1301 * any Solaris object with a dynamic section should be tagged as
1302 1302 * ELFOSABI_SOLARIS.
1303 1303 */
1304 1304 ofl->ofl_flags |= FLG_OF_OSABI;
1305 1305
1306 1306 return ((uintptr_t)ofl->ofl_osdynamic);
1307 1307 }
1308 1308
1309 1309 /*
1310 1310 * Build the GOT section and its associated relocation entries.
1311 1311 */
1312 1312 uintptr_t
1313 1313 ld_make_got(Ofl_desc *ofl)
1314 1314 {
1315 1315 Elf_Data *data;
1316 1316 Shdr *shdr;
1317 1317 Is_desc *isec;
1318 1318 size_t size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1319 1319 size_t rsize = (size_t)ofl->ofl_relocgotsz;
1320 1320
1321 1321 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1322 1322 &isec, &shdr, &data) == S_ERROR)
1323 1323 return (S_ERROR);
1324 1324
1325 1325 data->d_size = size;
1326 1326
1327 1327 shdr->sh_flags |= SHF_WRITE;
1328 1328 shdr->sh_size = (Xword)size;
1329 1329 shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1330 1330
1331 1331 ofl->ofl_osgot = ld_place_section(ofl, isec, NULL,
1332 1332 ld_targ.t_id.id_got, NULL);
1333 1333 if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1334 1334 return (S_ERROR);
1335 1335
1336 1336 ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1337 1337
1338 1338 return (1);
1339 1339 }
1340 1340
1341 1341 /*
1342 1342 * Build an interpreter section.
1343 1343 */
1344 1344 static uintptr_t
1345 1345 make_interp(Ofl_desc *ofl)
1346 1346 {
1347 1347 Shdr *shdr;
1348 1348 Elf_Data *data;
1349 1349 Is_desc *isec;
1350 1350 const char *iname = ofl->ofl_interp;
1351 1351 size_t size;
1352 1352
1353 1353 /*
1354 1354 * If -z nointerp is in effect, don't create an interpreter section.
1355 1355 */
1356 1356 if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1357 1357 return (1);
1358 1358
1359 1359 /*
1360 1360 * An .interp section is always created for a dynamic executable.
1361 1361 * A user can define the interpreter to use. This definition overrides
1362 1362 * the default that would be recorded in an executable, and triggers
1363 1363 * the creation of an .interp section in any other object. Presumably
1364 1364 * the user knows what they are doing. Refer to the generic ELF ABI
1365 1365 * section 5-4, and the ld(1) -I option.
1366 1366 */
1367 1367 if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1368 1368 FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1369 1369 return (1);
1370 1370
1371 1371 /*
1372 1372 * In the case of a dynamic executable, supply a default interpreter
1373 1373 * if the user has not specified their own.
1374 1374 */
1375 1375 if (iname == NULL)
1376 1376 iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1377 1377
1378 1378 size = strlen(iname) + 1;
1379 1379
1380 1380 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1381 1381 &isec, &shdr, &data) == S_ERROR)
1382 1382 return (S_ERROR);
1383 1383
1384 1384 data->d_size = size;
1385 1385 shdr->sh_size = (Xword)size;
1386 1386 data->d_align = shdr->sh_addralign = 1;
1387 1387
1388 1388 ofl->ofl_osinterp =
1389 1389 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_interp, NULL);
1390 1390 return ((uintptr_t)ofl->ofl_osinterp);
1391 1391 }
1392 1392
1393 1393 /*
1394 1394 * Common function used to build the SHT_SUNW_versym section, SHT_SUNW_syminfo
1395 1395 * section, and SHT_SUNW_capinfo section. Each of these sections provide
1396 1396 * additional symbol information, and their size parallels the associated
1397 1397 * symbol table.
1398 1398 */
1399 1399 static Os_desc *
1400 1400 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1401 1401 {
1402 1402 Shdr *shdr;
1403 1403 Elf_Data *data;
1404 1404 Is_desc *isec;
1405 1405
1406 1406 /*
1407 1407 * We don't know the size of this section yet, so set it to 0. The
1408 1408 * size gets filled in after the associated symbol table is sized.
1409 1409 */
1410 1410 if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1411 1411 S_ERROR)
1412 1412 return ((Os_desc *)S_ERROR);
1413 1413
1414 1414 return (ld_place_section(ofl, isec, NULL, ident, NULL));
1415 1415 }
1416 1416
1417 1417 /*
1418 1418 * Determine whether a symbol capability is redundant because the object
1419 1419 * capabilities are more restrictive.
1420 1420 */
1421 1421 inline static int
1422 1422 is_cap_redundant(Objcapset *ocapset, Objcapset *scapset)
1423 1423 {
1424 1424 Alist *oalp, *salp;
1425 1425 elfcap_mask_t omsk, smsk;
1426 1426
1427 1427 /*
1428 1428 * Inspect any platform capabilities. If the object defines platform
1429 1429 * capabilities, then the object will only be loaded for those
1430 1430 * platforms. A symbol capability set that doesn't define the same
1431 1431 * platforms is redundant, and a symbol capability that does not provide
1432 1432 * at least one platform name that matches a platform name in the object
1433 1433 * capabilities will never execute (as the object wouldn't have been
1434 1434 * loaded).
1435 1435 */
1436 1436 oalp = ocapset->oc_plat.cl_val;
1437 1437 salp = scapset->oc_plat.cl_val;
1438 1438 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1439 1439 return (1);
1440 1440
1441 1441 /*
1442 1442 * If the symbol capability set defines platforms, and the object
1443 1443 * doesn't, then the symbol set is more restrictive.
1444 1444 */
1445 1445 if (salp && (oalp == NULL))
1446 1446 return (0);
1447 1447
1448 1448 /*
1449 1449 * Next, inspect any machine name capabilities. If the object defines
1450 1450 * machine name capabilities, then the object will only be loaded for
1451 1451 * those machines. A symbol capability set that doesn't define the same
1452 1452 * machine names is redundant, and a symbol capability that does not
1453 1453 * provide at least one machine name that matches a machine name in the
1454 1454 * object capabilities will never execute (as the object wouldn't have
1455 1455 * been loaded).
1456 1456 */
1457 1457 oalp = ocapset->oc_plat.cl_val;
1458 1458 salp = scapset->oc_plat.cl_val;
1459 1459 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1460 1460 return (1);
1461 1461
1462 1462 /*
1463 1463 * If the symbol capability set defines machine names, and the object
1464 1464 * doesn't, then the symbol set is more restrictive.
1465 1465 */
1466 1466 if (salp && (oalp == NULL))
1467 1467 return (0);
1468 1468
1469 1469 /*
1470 1470 * Next, inspect any hardware capabilities. If the objects hardware
1471 1471 * capabilities are greater than or equal to that of the symbols
1472 1472 * capabilities, then the symbol capability set is redundant. If the
1473 1473 * symbols hardware capabilities are greater that the objects, then the
1474 1474 * symbol set is more restrictive.
1475 1475 *
1476 1476 * Note that this is a somewhat arbitrary definition, as each capability
1477 1477 * bit is independent of the others, and some of the higher order bits
1478 1478 * could be considered to be less important than lower ones. However,
1479 1479 * this is the only reasonable non-subjective definition.
1480 1480 */
1481 1481 omsk = ocapset->oc_hw_2.cm_val;
1482 1482 smsk = scapset->oc_hw_2.cm_val;
1483 1483 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1484 1484 return (1);
1485 1485 if (omsk < smsk)
1486 1486 return (0);
1487 1487
1488 1488 /*
1489 1489 * Finally, inspect the remaining hardware capabilities.
1490 1490 */
1491 1491 omsk = ocapset->oc_hw_1.cm_val;
1492 1492 smsk = scapset->oc_hw_1.cm_val;
1493 1493 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1494 1494 return (1);
1495 1495
1496 1496 return (0);
1497 1497 }
1498 1498
1499 1499 /*
1500 1500 * Capabilities values might have been assigned excluded values. These
1501 1501 * excluded values should be removed before calculating any capabilities
1502 1502 * sections size.
1503 1503 */
1504 1504 static void
1505 1505 capmask_value(Lm_list *lml, Word type, Capmask *capmask, int *title)
1506 1506 {
1507 1507 /*
1508 1508 * First determine whether any bits should be excluded.
1509 1509 */
1510 1510 if ((capmask->cm_val & capmask->cm_exc) == 0)
1511 1511 return;
1512 1512
1513 1513 DBG_CALL(Dbg_cap_post_title(lml, title));
1514 1514
1515 1515 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_CURRENT, type,
1516 1516 capmask->cm_val, ld_targ.t_m.m_mach));
1517 1517 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_EXCLUDE, type,
1518 1518 capmask->cm_exc, ld_targ.t_m.m_mach));
1519 1519
1520 1520 capmask->cm_val &= ~capmask->cm_exc;
1521 1521
1522 1522 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_RESOLVED, type,
1523 1523 capmask->cm_val, ld_targ.t_m.m_mach));
1524 1524 }
1525 1525
1526 1526 static void
1527 1527 capstr_value(Lm_list *lml, Word type, Caplist *caplist, int *title)
1528 1528 {
1529 1529 Aliste idx1, idx2;
1530 1530 char *estr;
1531 1531 Capstr *capstr;
1532 1532 Boolean found = FALSE;
1533 1533
1534 1534 /*
1535 1535 * First determine whether any strings should be excluded.
1536 1536 */
1537 1537 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1538 1538 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1539 1539 if (strcmp(estr, capstr->cs_str) == 0) {
1540 1540 found = TRUE;
1541 1541 break;
1542 1542 }
1543 1543 }
1544 1544 }
1545 1545
1546 1546 if (found == FALSE)
1547 1547 return;
1548 1548
1549 1549 /*
1550 1550 * Traverse the current strings, then delete the excluded strings,
1551 1551 * and finally display the resolved strings.
1552 1552 */
1553 1553 if (DBG_ENABLED) {
1554 1554 Dbg_cap_post_title(lml, title);
1555 1555 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1556 1556 Dbg_cap_ptr_entry(lml, DBG_STATE_CURRENT, type,
1557 1557 capstr->cs_str);
1558 1558 }
1559 1559 }
1560 1560 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1561 1561 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1562 1562 if (strcmp(estr, capstr->cs_str) == 0) {
1563 1563 DBG_CALL(Dbg_cap_ptr_entry(lml,
1564 1564 DBG_STATE_EXCLUDE, type, capstr->cs_str));
1565 1565 alist_delete(caplist->cl_val, &idx2);
1566 1566 break;
1567 1567 }
1568 1568 }
1569 1569 }
1570 1570 if (DBG_ENABLED) {
1571 1571 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1572 1572 Dbg_cap_ptr_entry(lml, DBG_STATE_RESOLVED, type,
1573 1573 capstr->cs_str);
1574 1574 }
1575 1575 }
1576 1576 }
1577 1577
1578 1578 /*
1579 1579 * Build a capabilities section.
1580 1580 */
1581 1581 #define CAP_UPDATE(cap, capndx, tag, val) \
1582 1582 cap->c_tag = tag; \
1583 1583 cap->c_un.c_val = val; \
1584 1584 cap++, capndx++;
1585 1585
1586 1586 static uintptr_t
1587 1587 make_cap(Ofl_desc *ofl, Word shtype, const char *shname, int ident)
1588 1588 {
1589 1589 Shdr *shdr;
1590 1590 Elf_Data *data;
1591 1591 Is_desc *isec;
1592 1592 Cap *cap;
1593 1593 size_t size = 0;
1594 1594 Word capndx = 0;
1595 1595 Str_tbl *strtbl;
1596 1596 Objcapset *ocapset = &ofl->ofl_ocapset;
1597 1597 Aliste idx1;
1598 1598 Capstr *capstr;
1599 1599 int title = 0;
1600 1600
1601 1601 /*
1602 1602 * Determine which string table to use for any CA_SUNW_MACH,
1603 1603 * CA_SUNW_PLAT, or CA_SUNW_ID strings.
1604 1604 */
1605 1605 if (OFL_IS_STATIC_OBJ(ofl))
1606 1606 strtbl = ofl->ofl_strtab;
1607 1607 else
1608 1608 strtbl = ofl->ofl_dynstrtab;
1609 1609
1610 1610 /*
1611 1611 * If symbol capabilities have been requested, but none have been
1612 1612 * created, warn the user. This scenario can occur if none of the
1613 1613 * input relocatable objects defined any object capabilities.
1614 1614 */
1615 1615 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && (ofl->ofl_capsymcnt == 0))
1616 1616 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1617 1617
1618 1618 /*
1619 1619 * If symbol capabilities have been collected, but no symbols are left
1620 1620 * referencing these capabilities, promote the capability groups back
1621 1621 * to an object capability definition.
1622 1622 */
1623 1623 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && ofl->ofl_capsymcnt &&
1624 1624 (ofl->ofl_capfamilies == NULL)) {
1625 1625 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1626 1626 ld_cap_move_symtoobj(ofl);
1627 1627 ofl->ofl_capsymcnt = 0;
1628 1628 ofl->ofl_capgroups = NULL;
1629 1629 ofl->ofl_flags &= ~FLG_OF_OTOSCAP;
1630 1630 }
1631 1631
1632 1632 /*
1633 1633 * Remove any excluded capabilities.
1634 1634 */
1635 1635 capstr_value(ofl->ofl_lml, CA_SUNW_PLAT, &ocapset->oc_plat, &title);
1636 1636 capstr_value(ofl->ofl_lml, CA_SUNW_MACH, &ocapset->oc_mach, &title);
1637 1637 capmask_value(ofl->ofl_lml, CA_SUNW_HW_2, &ocapset->oc_hw_2, &title);
1638 1638 capmask_value(ofl->ofl_lml, CA_SUNW_HW_1, &ocapset->oc_hw_1, &title);
1639 1639 capmask_value(ofl->ofl_lml, CA_SUNW_SF_1, &ocapset->oc_sf_1, &title);
1640 1640
1641 1641 /*
1642 1642 * Determine how many entries are required for any object capabilities.
1643 1643 */
1644 1644 size += alist_nitems(ocapset->oc_plat.cl_val);
1645 1645 size += alist_nitems(ocapset->oc_mach.cl_val);
1646 1646 if (ocapset->oc_hw_2.cm_val)
1647 1647 size++;
1648 1648 if (ocapset->oc_hw_1.cm_val)
1649 1649 size++;
1650 1650 if (ocapset->oc_sf_1.cm_val)
1651 1651 size++;
1652 1652
1653 1653 /*
1654 1654 * Only identify a capabilities group if the group has content. If a
1655 1655 * capabilities identifier exists, and no other capabilities have been
1656 1656 * supplied, remove the identifier. This scenario could exist if a
1657 1657 * user mistakenly defined a lone identifier, or if an identified group
1658 1658 * was overridden so as to clear the existing capabilities and the
1659 1659 * identifier was not also cleared.
1660 1660 */
1661 1661 if (ocapset->oc_id.cs_str) {
1662 1662 if (size)
1663 1663 size++;
1664 1664 else
1665 1665 ocapset->oc_id.cs_str = NULL;
1666 1666 }
1667 1667 if (size)
1668 1668 size++; /* Add CA_SUNW_NULL */
1669 1669
1670 1670 /*
1671 1671 * Determine how many entries are required for any symbol capabilities.
1672 1672 */
1673 1673 if (ofl->ofl_capsymcnt) {
1674 1674 /*
1675 1675 * If there are no object capabilities, a CA_SUNW_NULL entry
1676 1676 * is required before any symbol capabilities.
1677 1677 */
1678 1678 if (size == 0)
1679 1679 size++;
1680 1680 size += ofl->ofl_capsymcnt;
1681 1681 }
1682 1682
1683 1683 if (size == 0)
1684 1684 return (NULL);
1685 1685
1686 1686 if (new_section(ofl, shtype, shname, size, &isec,
1687 1687 &shdr, &data) == S_ERROR)
1688 1688 return (S_ERROR);
1689 1689
1690 1690 if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1691 1691 return (S_ERROR);
1692 1692
1693 1693 cap = (Cap *)data->d_buf;
1694 1694
1695 1695 /*
1696 1696 * Fill in any object capabilities. If there is an identifier, then the
1697 1697 * identifier comes first. The remaining items follow in precedence
1698 1698 * order, although the order isn't important for runtime verification.
1699 1699 */
1700 1700 if (ocapset->oc_id.cs_str) {
1701 1701 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1702 1702 if (st_insert(strtbl, ocapset->oc_id.cs_str) == -1)
1703 1703 return (S_ERROR);
1704 1704 ocapset->oc_id.cs_ndx = capndx;
1705 1705 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1706 1706 }
1707 1707 if (ocapset->oc_plat.cl_val) {
1708 1708 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1709 1709
1710 1710 /*
1711 1711 * Insert any platform name strings in the appropriate string
1712 1712 * table. The capability value can't be filled in yet, as the
1713 1713 * final offset of the strings isn't known until later.
1714 1714 */
1715 1715 for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx1, capstr)) {
1716 1716 if (st_insert(strtbl, capstr->cs_str) == -1)
1717 1717 return (S_ERROR);
1718 1718 capstr->cs_ndx = capndx;
1719 1719 CAP_UPDATE(cap, capndx, CA_SUNW_PLAT, 0);
1720 1720 }
1721 1721 }
1722 1722 if (ocapset->oc_mach.cl_val) {
1723 1723 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1724 1724
1725 1725 /*
1726 1726 * Insert the machine name strings in the appropriate string
1727 1727 * table. The capability value can't be filled in yet, as the
1728 1728 * final offset of the strings isn't known until later.
1729 1729 */
1730 1730 for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx1, capstr)) {
1731 1731 if (st_insert(strtbl, capstr->cs_str) == -1)
1732 1732 return (S_ERROR);
1733 1733 capstr->cs_ndx = capndx;
1734 1734 CAP_UPDATE(cap, capndx, CA_SUNW_MACH, 0);
1735 1735 }
1736 1736 }
1737 1737 if (ocapset->oc_hw_2.cm_val) {
1738 1738 ofl->ofl_flags |= FLG_OF_PTCAP;
1739 1739 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, ocapset->oc_hw_2.cm_val);
1740 1740 }
1741 1741 if (ocapset->oc_hw_1.cm_val) {
1742 1742 ofl->ofl_flags |= FLG_OF_PTCAP;
1743 1743 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, ocapset->oc_hw_1.cm_val);
1744 1744 }
1745 1745 if (ocapset->oc_sf_1.cm_val) {
1746 1746 ofl->ofl_flags |= FLG_OF_PTCAP;
1747 1747 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, ocapset->oc_sf_1.cm_val);
1748 1748 }
1749 1749 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1750 1750
1751 1751 /*
1752 1752 * Fill in any symbol capabilities.
1753 1753 */
1754 1754 if (ofl->ofl_capgroups) {
1755 1755 Cap_group *cgp;
1756 1756
1757 1757 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
1758 1758 Objcapset *scapset = &cgp->cg_set;
1759 1759 Aliste idx2;
1760 1760 Is_desc *isp;
1761 1761
1762 1762 cgp->cg_ndx = capndx;
1763 1763
1764 1764 if (scapset->oc_id.cs_str) {
1765 1765 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1766 1766 /*
1767 1767 * Insert the identifier string in the
1768 1768 * appropriate string table. The capability
1769 1769 * value can't be filled in yet, as the final
1770 1770 * offset of the string isn't known until later.
1771 1771 */
1772 1772 if (st_insert(strtbl,
1773 1773 scapset->oc_id.cs_str) == -1)
1774 1774 return (S_ERROR);
1775 1775 scapset->oc_id.cs_ndx = capndx;
1776 1776 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1777 1777 }
1778 1778
1779 1779 if (scapset->oc_plat.cl_val) {
1780 1780 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1781 1781
1782 1782 /*
1783 1783 * Insert the platform name string in the
1784 1784 * appropriate string table. The capability
1785 1785 * value can't be filled in yet, as the final
1786 1786 * offset of the string isn't known until later.
1787 1787 */
1788 1788 for (ALIST_TRAVERSE(scapset->oc_plat.cl_val,
1789 1789 idx2, capstr)) {
1790 1790 if (st_insert(strtbl,
1791 1791 capstr->cs_str) == -1)
1792 1792 return (S_ERROR);
1793 1793 capstr->cs_ndx = capndx;
1794 1794 CAP_UPDATE(cap, capndx,
1795 1795 CA_SUNW_PLAT, 0);
1796 1796 }
1797 1797 }
1798 1798 if (scapset->oc_mach.cl_val) {
1799 1799 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1800 1800
1801 1801 /*
1802 1802 * Insert the machine name string in the
1803 1803 * appropriate string table. The capability
1804 1804 * value can't be filled in yet, as the final
1805 1805 * offset of the string isn't known until later.
1806 1806 */
1807 1807 for (ALIST_TRAVERSE(scapset->oc_mach.cl_val,
1808 1808 idx2, capstr)) {
1809 1809 if (st_insert(strtbl,
1810 1810 capstr->cs_str) == -1)
1811 1811 return (S_ERROR);
1812 1812 capstr->cs_ndx = capndx;
1813 1813 CAP_UPDATE(cap, capndx,
1814 1814 CA_SUNW_MACH, 0);
1815 1815 }
1816 1816 }
1817 1817 if (scapset->oc_hw_2.cm_val) {
1818 1818 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2,
1819 1819 scapset->oc_hw_2.cm_val);
1820 1820 }
1821 1821 if (scapset->oc_hw_1.cm_val) {
1822 1822 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1,
1823 1823 scapset->oc_hw_1.cm_val);
1824 1824 }
1825 1825 if (scapset->oc_sf_1.cm_val) {
1826 1826 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1,
1827 1827 scapset->oc_sf_1.cm_val);
1828 1828 }
1829 1829 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1830 1830
1831 1831 /*
1832 1832 * If any object capabilities are available, determine
1833 1833 * whether these symbol capabilities are less
1834 1834 * restrictive, and hence redundant.
1835 1835 */
1836 1836 if (((ofl->ofl_flags & FLG_OF_PTCAP) == 0) ||
1837 1837 (is_cap_redundant(ocapset, scapset) == 0))
1838 1838 continue;
1839 1839
1840 1840 /*
1841 1841 * Indicate any files that provide redundant symbol
1842 1842 * capabilities.
1843 1843 */
1844 1844 for (APLIST_TRAVERSE(cgp->cg_secs, idx2, isp)) {
1845 1845 ld_eprintf(ofl, ERR_WARNING,
1846 1846 MSG_INTL(MSG_CAP_REDUNDANT),
1847 1847 isp->is_file->ifl_name,
1848 1848 EC_WORD(isp->is_scnndx), isp->is_name);
1849 1849 }
1850 1850 }
1851 1851 }
1852 1852
1853 1853 /*
1854 1854 * If capabilities strings are required, the sh_info field of the
1855 1855 * section header will be set to the associated string table.
1856 1856 */
1857 1857 if (ofl->ofl_flags & FLG_OF_CAPSTRS)
1858 1858 shdr->sh_flags |= SHF_INFO_LINK;
1859 1859
1860 1860 /*
1861 1861 * Place these capabilities in the output file.
1862 1862 */
1863 1863 if ((ofl->ofl_oscap = ld_place_section(ofl, isec,
1864 1864 NULL, ident, NULL)) == (Os_desc *)S_ERROR)
1865 1865 return (S_ERROR);
1866 1866
1867 1867 /*
1868 1868 * If symbol capabilities are required, then a .SUNW_capinfo section is
1869 1869 * also created. This table will eventually be sized to match the
1870 1870 * associated symbol table.
1871 1871 */
1872 1872 if (ofl->ofl_capfamilies) {
1873 1873 if ((ofl->ofl_oscapinfo = make_sym_sec(ofl,
1874 1874 MSG_ORIG(MSG_SCN_SUNWCAPINFO), SHT_SUNW_capinfo,
1875 1875 ld_targ.t_id.id_capinfo)) == (Os_desc *)S_ERROR)
1876 1876 return (S_ERROR);
1877 1877
1878 1878 /*
1879 1879 * If we're generating a dynamic object, capabilities family
1880 1880 * members are maintained in a .SUNW_capchain section.
1881 1881 */
1882 1882 if (ofl->ofl_capchaincnt &&
1883 1883 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1884 1884 if (new_section(ofl, SHT_SUNW_capchain,
1885 1885 MSG_ORIG(MSG_SCN_SUNWCAPCHAIN),
1886 1886 ofl->ofl_capchaincnt, &isec, &shdr,
1887 1887 &data) == S_ERROR)
1888 1888 return (S_ERROR);
1889 1889
1890 1890 ofl->ofl_oscapchain = ld_place_section(ofl, isec,
1891 1891 NULL, ld_targ.t_id.id_capchain, NULL);
1892 1892 if (ofl->ofl_oscapchain == (Os_desc *)S_ERROR)
1893 1893 return (S_ERROR);
1894 1894
1895 1895 }
1896 1896 }
1897 1897 return (1);
1898 1898 }
1899 1899 #undef CAP_UPDATE
1900 1900
1901 1901 /*
1902 1902 * Build the PLT section and its associated relocation entries.
1903 1903 */
1904 1904 static uintptr_t
1905 1905 make_plt(Ofl_desc *ofl)
1906 1906 {
1907 1907 Shdr *shdr;
1908 1908 Elf_Data *data;
1909 1909 Is_desc *isec;
1910 1910 size_t size = ld_targ.t_m.m_plt_reservsz +
1911 1911 (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1912 1912 ld_targ.t_m.m_plt_entsize);
1913 1913 size_t rsize = (size_t)ofl->ofl_relocpltsz;
1914 1914
1915 1915 /*
1916 1916 * On sparc, account for the NOP at the end of the plt.
1917 1917 */
1918 1918 if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1919 1919 size += sizeof (Word);
1920 1920
1921 1921 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1922 1922 &isec, &shdr, &data) == S_ERROR)
1923 1923 return (S_ERROR);
1924 1924
1925 1925 data->d_size = size;
1926 1926 data->d_align = ld_targ.t_m.m_plt_align;
1927 1927
1928 1928 shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1929 1929 shdr->sh_size = (Xword)size;
1930 1930 shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1931 1931 shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1932 1932
1933 1933 ofl->ofl_osplt = ld_place_section(ofl, isec, NULL,
1934 1934 ld_targ.t_id.id_plt, NULL);
1935 1935 if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1936 1936 return (S_ERROR);
1937 1937
1938 1938 ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1939 1939
1940 1940 return (1);
1941 1941 }
1942 1942
1943 1943 /*
1944 1944 * Make the hash table. Only built for dynamic executables and shared
1945 1945 * libraries, and provides hashed lookup into the global symbol table
1946 1946 * (.dynsym) for the run-time linker to resolve symbol lookups.
1947 1947 */
1948 1948 static uintptr_t
1949 1949 make_hash(Ofl_desc *ofl)
1950 1950 {
1951 1951 Shdr *shdr;
1952 1952 Elf_Data *data;
1953 1953 Is_desc *isec;
1954 1954 size_t size;
1955 1955 Word nsyms = ofl->ofl_globcnt;
1956 1956 size_t cnt;
1957 1957
1958 1958 /*
1959 1959 * Allocate section header structures. We set entcnt to 0
1960 1960 * because it's going to change after we place this section.
1961 1961 */
1962 1962 if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1963 1963 &isec, &shdr, &data) == S_ERROR)
1964 1964 return (S_ERROR);
1965 1965
1966 1966 /*
1967 1967 * Place the section first since it will affect the local symbol
1968 1968 * count.
1969 1969 */
1970 1970 ofl->ofl_oshash =
1971 1971 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_hash, NULL);
1972 1972 if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1973 1973 return (S_ERROR);
1974 1974
1975 1975 /*
1976 1976 * Calculate the number of output hash buckets.
1977 1977 */
1978 1978 ofl->ofl_hashbkts = findprime(nsyms);
1979 1979
1980 1980 /*
1981 1981 * The size of the hash table is determined by
1982 1982 *
1983 1983 * i. the initial nbucket and nchain entries (2)
1984 1984 * ii. the number of buckets (calculated above)
1985 1985 * iii. the number of chains (this is based on the number of
1986 1986 * symbols in the .dynsym array).
1987 1987 */
1988 1988 cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl);
1989 1989 size = cnt * shdr->sh_entsize;
1990 1990
1991 1991 /*
1992 1992 * Finalize the section header and data buffer initialization.
1993 1993 */
1994 1994 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
1995 1995 return (S_ERROR);
1996 1996 data->d_size = size;
1997 1997 shdr->sh_size = (Xword)size;
1998 1998
1999 1999 return (1);
2000 2000 }
2001 2001
2002 2002 /*
2003 2003 * Generate the standard symbol table. Contains all locals and globals,
2004 2004 * and resides in a non-allocatable section (ie. it can be stripped).
2005 2005 */
2006 2006 static uintptr_t
2007 2007 make_symtab(Ofl_desc *ofl)
2008 2008 {
2009 2009 Shdr *shdr;
2010 2010 Elf_Data *data;
2011 2011 Is_desc *isec;
2012 2012 Is_desc *xisec = 0;
2013 2013 size_t size;
2014 2014 Word symcnt;
2015 2015
2016 2016 /*
2017 2017 * Create the section headers. Note that we supply an ent_cnt
2018 2018 * of 0. We won't know the count until the section has been placed.
2019 2019 */
2020 2020 if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
2021 2021 &isec, &shdr, &data) == S_ERROR)
2022 2022 return (S_ERROR);
2023 2023
2024 2024 /*
2025 2025 * Place the section first since it will affect the local symbol
2026 2026 * count.
2027 2027 */
2028 2028 if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, NULL,
2029 2029 ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR)
2030 2030 return (S_ERROR);
2031 2031
2032 2032 /*
2033 2033 * At this point we've created all but the 'shstrtab' section.
2034 2034 * Determine if we have to use 'Extended Sections'. If so - then
2035 2035 * also create a SHT_SYMTAB_SHNDX section.
2036 2036 */
2037 2037 if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
2038 2038 Shdr *xshdr;
2039 2039 Elf_Data *xdata;
2040 2040
2041 2041 if (new_section(ofl, SHT_SYMTAB_SHNDX,
2042 2042 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
2043 2043 &xshdr, &xdata) == S_ERROR)
2044 2044 return (S_ERROR);
2045 2045
2046 2046 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, NULL,
2047 2047 ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR)
2048 2048 return (S_ERROR);
2049 2049 }
2050 2050
2051 2051 /*
2052 2052 * Calculated number of symbols, which need to be augmented by
2053 2053 * the (yet to be created) .shstrtab entry.
2054 2054 */
2055 2055 symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl));
2056 2056 size = symcnt * shdr->sh_entsize;
2057 2057
2058 2058 /*
2059 2059 * Finalize the section header and data buffer initialization.
2060 2060 */
2061 2061 data->d_size = size;
2062 2062 shdr->sh_size = (Xword)size;
2063 2063
2064 2064 /*
2065 2065 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
2066 2066 */
2067 2067 if (xisec) {
2068 2068 size_t xsize = symcnt * sizeof (Word);
2069 2069
2070 2070 xisec->is_indata->d_size = xsize;
2071 2071 xisec->is_shdr->sh_size = (Xword)xsize;
2072 2072 }
2073 2073
2074 2074 return (1);
2075 2075 }
2076 2076
2077 2077 /*
2078 2078 * Build a dynamic symbol table. These tables reside in the text
2079 2079 * segment of a dynamic executable or shared library.
2080 2080 *
2081 2081 * .SUNW_ldynsym contains local function symbols
2082 2082 * .dynsym contains only globals symbols
2083 2083 *
2084 2084 * The two tables are created adjacent to each other, with .SUNW_ldynsym
2085 2085 * coming first.
2086 2086 */
2087 2087 static uintptr_t
2088 2088 make_dynsym(Ofl_desc *ofl)
2089 2089 {
2090 2090 Shdr *shdr, *lshdr;
2091 2091 Elf_Data *data, *ldata;
2092 2092 Is_desc *isec, *lisec;
2093 2093 size_t size;
2094 2094 Xword cnt;
2095 2095 int allow_ldynsym;
2096 2096
2097 2097 /*
2098 2098 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
2099 2099 * when it is allowed by the file type, even if the resulting
2100 2100 * table only ends up with a single STT_FILE in it. There are
2101 2101 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
2102 2102 * entry in the .dynamic section, which is something we would
2103 2103 * like to encourage, and (2) Without it, we cannot generate
2104 2104 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
2105 2105 * value to DTrace.
2106 2106 *
2107 2107 * In practice, it is extremely rare for an object not to have
2108 2108 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
2109 2109 * doing it anyway.
2110 2110 */
2111 2111 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
2112 2112
2113 2113 /*
2114 2114 * Create the section headers. Note that we supply an ent_cnt
2115 2115 * of 0. We won't know the count until the section has been placed.
2116 2116 */
2117 2117 if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
2118 2118 MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
2119 2119 return (S_ERROR);
2120 2120
2121 2121 if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
2122 2122 &isec, &shdr, &data) == S_ERROR)
2123 2123 return (S_ERROR);
2124 2124
2125 2125 /*
2126 2126 * Place the section(s) first since it will affect the local symbol
2127 2127 * count.
2128 2128 */
2129 2129 if (allow_ldynsym &&
2130 2130 ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, NULL,
2131 2131 ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR))
2132 2132 return (S_ERROR);
2133 2133 ofl->ofl_osdynsym =
2134 2134 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynsym, NULL);
2135 2135 if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
2136 2136 return (S_ERROR);
2137 2137
2138 2138 cnt = DYNSYM_ALL_CNT(ofl);
2139 2139 size = (size_t)cnt * shdr->sh_entsize;
2140 2140
2141 2141 /*
2142 2142 * Finalize the section header and data buffer initialization.
2143 2143 */
2144 2144 data->d_size = size;
2145 2145 shdr->sh_size = (Xword)size;
2146 2146
2147 2147 /*
2148 2148 * An ldynsym contains local function symbols. It is not
2149 2149 * used for linking, but if present, serves to allow better
2150 2150 * stack traces to be generated in contexts where the symtab
2151 2151 * is not available. (dladdr(), or stripped executable/library files).
2152 2152 */
2153 2153 if (allow_ldynsym) {
2154 2154 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
2155 2155 size = (size_t)cnt * shdr->sh_entsize;
2156 2156
2157 2157 ldata->d_size = size;
2158 2158 lshdr->sh_size = (Xword)size;
2159 2159 }
2160 2160
2161 2161 return (1);
2162 2162 }
2163 2163
2164 2164 /*
2165 2165 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
2166 2166 * index sections for the .SUNW_ldynsym/.dynsym pair that present data
2167 2167 * and function symbols sorted by address.
2168 2168 */
2169 2169 static uintptr_t
2170 2170 make_dynsort(Ofl_desc *ofl)
2171 2171 {
2172 2172 Shdr *shdr;
2173 2173 Elf_Data *data;
2174 2174 Is_desc *isec;
2175 2175
2176 2176 /* Only do it if the .SUNW_ldynsym section is present */
2177 2177 if (!OFL_ALLOW_LDYNSYM(ofl))
2178 2178 return (1);
2179 2179
2180 2180 /* .SUNW_dynsymsort */
2181 2181 if (ofl->ofl_dynsymsortcnt > 0) {
2182 2182 if (new_section(ofl, SHT_SUNW_symsort,
2183 2183 MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
2184 2184 &isec, &shdr, &data) == S_ERROR)
2185 2185 return (S_ERROR);
2186 2186
2187 2187 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, NULL,
2188 2188 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2189 2189 return (S_ERROR);
2190 2190 }
2191 2191
2192 2192 /* .SUNW_dyntlssort */
2193 2193 if (ofl->ofl_dyntlssortcnt > 0) {
2194 2194 if (new_section(ofl, SHT_SUNW_tlssort,
2195 2195 MSG_ORIG(MSG_SCN_DYNTLSSORT),
2196 2196 ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
2197 2197 return (S_ERROR);
2198 2198
2199 2199 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, NULL,
2200 2200 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2201 2201 return (S_ERROR);
2202 2202 }
2203 2203
2204 2204 return (1);
2205 2205 }
2206 2206
2207 2207 /*
2208 2208 * Helper routine for make_dynsym_shndx. Builds a
2209 2209 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
2210 2210 * which one it is.
2211 2211 */
2212 2212 static uintptr_t
2213 2213 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
2214 2214 Os_desc **ret_os)
2215 2215 {
2216 2216 Is_desc *isec;
2217 2217 Is_desc *dynsymisp;
2218 2218 Shdr *shdr, *dynshdr;
2219 2219 Elf_Data *data;
2220 2220
2221 2221 dynsymisp = ld_os_first_isdesc(symtab);
2222 2222 dynshdr = dynsymisp->is_shdr;
2223 2223
2224 2224 if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
2225 2225 (dynshdr->sh_size / dynshdr->sh_entsize),
2226 2226 &isec, &shdr, &data) == S_ERROR)
2227 2227 return (S_ERROR);
2228 2228
2229 2229 if ((*ret_os = ld_place_section(ofl, isec, NULL,
2230 2230 ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR)
2231 2231 return (S_ERROR);
2232 2232
2233 2233 assert(*ret_os);
2234 2234
2235 2235 return (1);
2236 2236 }
2237 2237
2238 2238 /*
2239 2239 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
2240 2240 */
2241 2241 static uintptr_t
2242 2242 make_dynsym_shndx(Ofl_desc *ofl)
2243 2243 {
2244 2244 /*
2245 2245 * If there is a .SUNW_ldynsym, generate a section for its extended
2246 2246 * index section as well.
2247 2247 */
2248 2248 if (OFL_ALLOW_LDYNSYM(ofl)) {
2249 2249 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
2250 2250 ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
2251 2251 return (S_ERROR);
2252 2252 }
2253 2253
2254 2254 /* The Generate a section for the dynsym */
2255 2255 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
2256 2256 ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
2257 2257 return (S_ERROR);
2258 2258
2259 2259 return (1);
2260 2260 }
2261 2261
2262 2262
2263 2263 /*
2264 2264 * Build a string table for the section headers.
2265 2265 */
2266 2266 static uintptr_t
2267 2267 make_shstrtab(Ofl_desc *ofl)
2268 2268 {
2269 2269 Shdr *shdr;
2270 2270 Elf_Data *data;
2271 2271 Is_desc *isec;
2272 2272 size_t size;
2273 2273
2274 2274 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
2275 2275 0, &isec, &shdr, &data) == S_ERROR)
2276 2276 return (S_ERROR);
2277 2277
2278 2278 /*
2279 2279 * Place the section first, as it may effect the number of section
2280 2280 * headers to account for.
2281 2281 */
2282 2282 ofl->ofl_osshstrtab =
2283 2283 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_note, NULL);
2284 2284 if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
2285 2285 return (S_ERROR);
2286 2286
2287 2287 size = st_getstrtab_sz(ofl->ofl_shdrsttab);
2288 2288 assert(size > 0);
2289 2289
2290 2290 data->d_size = size;
2291 2291 shdr->sh_size = (Xword)size;
2292 2292
2293 2293 return (1);
2294 2294 }
2295 2295
2296 2296 /*
2297 2297 * Build a string section for the standard symbol table.
2298 2298 */
2299 2299 static uintptr_t
2300 2300 make_strtab(Ofl_desc *ofl)
2301 2301 {
2302 2302 Shdr *shdr;
2303 2303 Elf_Data *data;
2304 2304 Is_desc *isec;
2305 2305 size_t size;
2306 2306
2307 2307 /*
2308 2308 * This string table consists of all the global and local symbols.
2309 2309 * Account for null bytes at end of the file name and the beginning
2310 2310 * of section.
2311 2311 */
2312 2312 if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
2313 2313 return (S_ERROR);
2314 2314
2315 2315 size = st_getstrtab_sz(ofl->ofl_strtab);
2316 2316 assert(size > 0);
2317 2317
2318 2318 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
2319 2319 0, &isec, &shdr, &data) == S_ERROR)
2320 2320 return (S_ERROR);
2321 2321
2322 2322 /* Set the size of the data area */
2323 2323 data->d_size = size;
2324 2324 shdr->sh_size = (Xword)size;
2325 2325
2326 2326 ofl->ofl_osstrtab =
2327 2327 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_strtab, NULL);
2328 2328 return ((uintptr_t)ofl->ofl_osstrtab);
2329 2329 }
2330 2330
2331 2331 /*
2332 2332 * Build a string table for the dynamic symbol table.
2333 2333 */
2334 2334 static uintptr_t
2335 2335 make_dynstr(Ofl_desc *ofl)
2336 2336 {
2337 2337 Shdr *shdr;
2338 2338 Elf_Data *data;
2339 2339 Is_desc *isec;
2340 2340 size_t size;
2341 2341
2342 2342 /*
2343 2343 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
2344 2344 * symbol that precedes the scope reduced global symbols.
2345 2345 */
2346 2346 if (OFL_ALLOW_LDYNSYM(ofl)) {
2347 2347 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
2348 2348 return (S_ERROR);
2349 2349 ofl->ofl_dynscopecnt++;
2350 2350 }
2351 2351
2352 2352 /*
2353 2353 * Account for any local, named register symbols. These locals are
2354 2354 * required for reference from DT_REGISTER .dynamic entries.
2355 2355 */
2356 2356 if (ofl->ofl_regsyms) {
2357 2357 int ndx;
2358 2358
2359 2359 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2360 2360 Sym_desc *sdp;
2361 2361
2362 2362 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2363 2363 continue;
2364 2364
2365 2365 if (!SYM_IS_HIDDEN(sdp) &&
2366 2366 (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
2367 2367 continue;
2368 2368
2369 2369 if (sdp->sd_sym->st_name == NULL)
2370 2370 continue;
2371 2371
2372 2372 if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
2373 2373 return (S_ERROR);
2374 2374 }
2375 2375 }
2376 2376
2377 2377 /*
2378 2378 * Reserve entries for any per-symbol auxiliary/filter strings.
2379 2379 */
2380 2380 if (ofl->ofl_dtsfltrs != NULL) {
2381 2381 Dfltr_desc *dftp;
2382 2382 Aliste idx;
2383 2383
2384 2384 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
2385 2385 if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
2386 2386 return (S_ERROR);
2387 2387 }
2388 2388
2389 2389 size = st_getstrtab_sz(ofl->ofl_dynstrtab);
2390 2390 assert(size > 0);
2391 2391
2392 2392 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
2393 2393 0, &isec, &shdr, &data) == S_ERROR)
2394 2394 return (S_ERROR);
2395 2395
2396 2396 /* Make it allocable if necessary */
2397 2397 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2398 2398 shdr->sh_flags |= SHF_ALLOC;
2399 2399
2400 2400 /* Set the size of the data area */
2401 2401 data->d_size = size + DYNSTR_EXTRA_PAD;
2402 2402
2403 2403 shdr->sh_size = (Xword)size;
2404 2404
2405 2405 ofl->ofl_osdynstr =
2406 2406 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynstr, NULL);
2407 2407 return ((uintptr_t)ofl->ofl_osdynstr);
2408 2408 }
2409 2409
2410 2410 /*
2411 2411 * Generate an output relocation section which will contain the relocation
2412 2412 * information to be applied to the `osp' section.
2413 2413 *
2414 2414 * If (osp == NULL) then we are creating the coalesced relocation section
2415 2415 * for an executable and/or a shared object.
2416 2416 */
2417 2417 static uintptr_t
2418 2418 make_reloc(Ofl_desc *ofl, Os_desc *osp)
2419 2419 {
2420 2420 Shdr *shdr;
2421 2421 Elf_Data *data;
2422 2422 Is_desc *isec;
2423 2423 size_t size;
2424 2424 Xword sh_flags;
2425 2425 char *sectname;
2426 2426 Os_desc *rosp;
2427 2427 Word relsize;
2428 2428 const char *rel_prefix;
2429 2429
2430 2430 /* LINTED */
2431 2431 if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
2432 2432 /* REL */
2433 2433 relsize = sizeof (Rel);
2434 2434 rel_prefix = MSG_ORIG(MSG_SCN_REL);
2435 2435 } else {
2436 2436 /* RELA */
2437 2437 relsize = sizeof (Rela);
2438 2438 rel_prefix = MSG_ORIG(MSG_SCN_RELA);
2439 2439 }
2440 2440
2441 2441 if (osp) {
2442 2442 size = osp->os_szoutrels;
2443 2443 sh_flags = osp->os_shdr->sh_flags;
2444 2444 if ((sectname = libld_malloc(strlen(rel_prefix) +
2445 2445 strlen(osp->os_name) + 1)) == 0)
2446 2446 return (S_ERROR);
2447 2447 (void) strcpy(sectname, rel_prefix);
2448 2448 (void) strcat(sectname, osp->os_name);
2449 2449 } else if (ofl->ofl_flags & FLG_OF_COMREL) {
2450 2450 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
2451 2451 sh_flags = SHF_ALLOC;
2452 2452 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
2453 2453 } else {
2454 2454 size = ofl->ofl_relocrelsz;
2455 2455 sh_flags = SHF_ALLOC;
2456 2456 sectname = (char *)rel_prefix;
2457 2457 }
2458 2458
2459 2459 /*
2460 2460 * Keep track of total size of 'output relocations' (to be stored
2461 2461 * in .dynamic)
2462 2462 */
2463 2463 /* LINTED */
2464 2464 ofl->ofl_relocsz += (Xword)size;
2465 2465
2466 2466 if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
2467 2467 &shdr, &data) == S_ERROR)
2468 2468 return (S_ERROR);
2469 2469
2470 2470 data->d_size = size;
2471 2471
2472 2472 shdr->sh_size = (Xword)size;
2473 2473 if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
2474 2474 shdr->sh_flags = SHF_ALLOC;
2475 2475
2476 2476 if (osp) {
2477 2477 /*
2478 2478 * The sh_info field of the SHT_REL* sections points to the
2479 2479 * section the relocations are to be applied to.
2480 2480 */
2481 2481 shdr->sh_flags |= SHF_INFO_LINK;
2482 2482 }
2483 2483
2484 2484 rosp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_rel, NULL);
2485 2485 if (rosp == (Os_desc *)S_ERROR)
2486 2486 return (S_ERROR);
2487 2487
2488 2488 /*
2489 2489 * Associate this relocation section to the section its going to
2490 2490 * relocate.
2491 2491 */
2492 2492 if (osp) {
2493 2493 Aliste idx;
2494 2494 Is_desc *risp;
2495 2495
2496 2496 /*
2497 2497 * This is used primarily so that we can update
2498 2498 * SHT_GROUP[sect_no] entries to point to the
2499 2499 * created output relocation sections.
2500 2500 */
2501 2501 for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
2502 2502 risp->is_osdesc = rosp;
2503 2503
2504 2504 /*
2505 2505 * If the input relocation section had the SHF_GROUP
2506 2506 * flag set - propagate it to the output relocation
2507 2507 * section.
2508 2508 */
2509 2509 if (risp->is_shdr->sh_flags & SHF_GROUP) {
2510 2510 rosp->os_shdr->sh_flags |= SHF_GROUP;
2511 2511 break;
2512 2512 }
2513 2513 }
2514 2514 osp->os_relosdesc = rosp;
2515 2515 } else
2516 2516 ofl->ofl_osrel = rosp;
2517 2517
2518 2518 /*
2519 2519 * If this is the first relocation section we've encountered save it
2520 2520 * so that the .dynamic entry can be initialized accordingly.
2521 2521 */
2522 2522 if (ofl->ofl_osrelhead == (Os_desc *)0)
2523 2523 ofl->ofl_osrelhead = rosp;
2524 2524
2525 2525 return (1);
2526 2526 }
2527 2527
2528 2528 /*
2529 2529 * Generate version needed section.
2530 2530 */
2531 2531 static uintptr_t
2532 2532 make_verneed(Ofl_desc *ofl)
2533 2533 {
2534 2534 Shdr *shdr;
2535 2535 Elf_Data *data;
2536 2536 Is_desc *isec;
2537 2537
2538 2538 /*
2539 2539 * verneed sections do not have a constant element size, so the
2540 2540 * value of ent_cnt specified here (0) is meaningless.
2541 2541 */
2542 2542 if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2543 2543 0, &isec, &shdr, &data) == S_ERROR)
2544 2544 return (S_ERROR);
2545 2545
2546 2546 /* During version processing we calculated the total size. */
2547 2547 data->d_size = ofl->ofl_verneedsz;
2548 2548 shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2549 2549
2550 2550 ofl->ofl_osverneed =
2551 2551 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2552 2552 return ((uintptr_t)ofl->ofl_osverneed);
2553 2553 }
2554 2554
2555 2555 /*
2556 2556 * Generate a version definition section.
2557 2557 *
2558 2558 * o the SHT_SUNW_verdef section defines the versions that exist within this
2559 2559 * image.
2560 2560 */
2561 2561 static uintptr_t
2562 2562 make_verdef(Ofl_desc *ofl)
2563 2563 {
2564 2564 Shdr *shdr;
2565 2565 Elf_Data *data;
2566 2566 Is_desc *isec;
2567 2567 Ver_desc *vdp;
2568 2568 Str_tbl *strtab;
2569 2569
2570 2570 /*
2571 2571 * Reserve a string table entry for the base version dependency (other
2572 2572 * dependencies have symbol representations, which will already be
2573 2573 * accounted for during symbol processing).
2574 2574 */
2575 2575 vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
2576 2576
2577 2577 if (OFL_IS_STATIC_OBJ(ofl))
2578 2578 strtab = ofl->ofl_strtab;
2579 2579 else
2580 2580 strtab = ofl->ofl_dynstrtab;
2581 2581
2582 2582 if (st_insert(strtab, vdp->vd_name) == -1)
2583 2583 return (S_ERROR);
2584 2584
2585 2585 /*
2586 2586 * verdef sections do not have a constant element size, so the
2587 2587 * value of ent_cnt specified here (0) is meaningless.
2588 2588 */
2589 2589 if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2590 2590 0, &isec, &shdr, &data) == S_ERROR)
2591 2591 return (S_ERROR);
2592 2592
2593 2593 /* During version processing we calculated the total size. */
2594 2594 data->d_size = ofl->ofl_verdefsz;
2595 2595 shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2596 2596
2597 2597 ofl->ofl_osverdef =
2598 2598 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2599 2599 return ((uintptr_t)ofl->ofl_osverdef);
2600 2600 }
2601 2601
2602 2602 /*
2603 2603 * This routine is called when -z nopartial is in effect.
2604 2604 */
2605 2605 uintptr_t
2606 2606 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2607 2607 {
2608 2608 Shdr *shdr;
2609 2609 Elf_Data *data;
2610 2610 Is_desc *isec;
2611 2611 Os_desc *osp;
2612 2612
2613 2613 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2614 2614 &isec, &shdr, &data) == S_ERROR)
2615 2615 return (S_ERROR);
2616 2616
2617 2617 shdr->sh_flags |= SHF_WRITE;
2618 2618 data->d_size = size;
2619 2619 shdr->sh_size = (Xword)size;
2620 2620 if (align != 0) {
2621 2621 data->d_align = align;
2622 2622 shdr->sh_addralign = align;
2623 2623 }
2624 2624
2625 2625 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2626 2626 return (S_ERROR);
2627 2627
2628 2628 /*
2629 2629 * Retain handle to this .data input section. Variables using move
2630 2630 * sections (partial initialization) will be redirected here when
2631 2631 * such global references are added and '-z nopartial' is in effect.
2632 2632 */
2633 2633 ofl->ofl_isparexpn = isec;
2634 2634 osp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_data, NULL);
2635 2635 if (osp == (Os_desc *)S_ERROR)
2636 2636 return (S_ERROR);
2637 2637
2638 2638 if (!(osp->os_flags & FLG_OS_OUTREL)) {
2639 2639 ofl->ofl_dynshdrcnt++;
2640 2640 osp->os_flags |= FLG_OS_OUTREL;
2641 2641 }
2642 2642 return (1);
2643 2643 }
2644 2644
2645 2645 /*
2646 2646 * Make .sunwmove section
2647 2647 */
2648 2648 uintptr_t
2649 2649 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2650 2650 {
2651 2651 Shdr *shdr;
2652 2652 Elf_Data *data;
2653 2653 Is_desc *isec;
2654 2654 Aliste idx;
2655 2655 Sym_desc *sdp;
2656 2656 int cnt = 1;
2657 2657
2658 2658
2659 2659 if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2660 2660 mv_nums, &isec, &shdr, &data) == S_ERROR)
2661 2661 return (S_ERROR);
2662 2662
2663 2663 if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL)
2664 2664 return (S_ERROR);
2665 2665
2666 2666 /*
2667 2667 * Copy move entries
2668 2668 */
2669 2669 for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) {
2670 2670 Aliste idx2;
2671 2671 Mv_desc *mdp;
2672 2672
2673 2673 if (sdp->sd_flags & FLG_SY_PAREXPN)
2674 2674 continue;
2675 2675
2676 2676 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp))
2677 2677 mdp->md_oidx = cnt++;
2678 2678 }
2679 2679
2680 2680 if ((ofl->ofl_osmove = ld_place_section(ofl, isec, NULL, 0, NULL)) ==
2681 2681 (Os_desc *)S_ERROR)
2682 2682 return (S_ERROR);
2683 2683
2684 2684 return (1);
2685 2685 }
2686 2686
2687 2687 /*
2688 2688 * Given a relocation descriptor that references a string table
2689 2689 * input section, locate the string referenced and return a pointer
2690 2690 * to it.
2691 2691 */
2692 2692 static const char *
2693 2693 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2694 2694 {
2695 2695 Sym_desc *sdp = rsp->rel_sym;
2696 2696 Xword str_off;
2697 2697
2698 2698 /*
2699 2699 * In the case of an STT_SECTION symbol, the addend of the
2700 2700 * relocation gives the offset into the string section. For
2701 2701 * other symbol types, the symbol value is the offset.
2702 2702 */
2703 2703
2704 2704 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2705 2705 str_off = sdp->sd_sym->st_value;
2706 2706 } else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2707 2707 /*
2708 2708 * For SHT_RELA, the addend value is found in the
2709 2709 * rel_raddend field of the relocation.
2710 2710 */
2711 2711 str_off = rsp->rel_raddend;
2712 2712 } else { /* REL and STT_SECTION */
2713 2713 /*
2714 2714 * For SHT_REL, the "addend" is not part of the relocation
2715 2715 * record. Instead, it is found at the relocation target
2716 2716 * address.
2717 2717 */
2718 2718 uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2719 2719 (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2720 2720
2721 2721 if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2722 2722 return (0);
2723 2723 }
2724 2724
2725 2725 return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2726 2726 }
2727 2727
2728 2728 /*
2729 2729 * First pass over the relocation records for string table merging.
2730 2730 * Build lists of relocations and symbols that will need modification,
2731 2731 * and insert the strings they reference into the mstrtab string table.
2732 2732 *
2733 2733 * entry:
2734 2734 * ofl, osp - As passed to ld_make_strmerge().
2735 2735 * mstrtab - String table to receive input strings. This table
2736 2736 * must be in its first (initialization) pass and not
2737 2737 * yet cooked (st_getstrtab_sz() not yet called).
2738 2738 * rel_alpp - APlist to receive pointer to any relocation
2739 2739 * descriptors with STT_SECTION symbols that reference
2740 2740 * one of the input sections being merged.
2741 2741 * sym_alpp - APlist to receive pointer to any symbols that reference
2742 2742 * one of the input sections being merged.
2743 2743 * rcp - Pointer to cache of relocation descriptors to examine.
2744 2744 * Either &ofl->ofl_actrels (active relocations)
2745 2745 * or &ofl->ofl_outrels (output relocations).
2746 2746 *
2747 2747 * exit:
2748 2748 * On success, rel_alpp and sym_alpp are updated, and
2749 2749 * any strings in the mergeable input sections referenced by
2750 2750 * a relocation has been entered into mstrtab. True (1) is returned.
2751 2751 *
2752 2752 * On failure, False (0) is returned.
2753 2753 */
2754 2754 static int
2755 2755 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2756 2756 APlist **rel_alpp, APlist **sym_alpp, Rel_cache *rcp)
2757 2757 {
2758 2758 Aliste idx;
2759 2759 Rel_cachebuf *rcbp;
2760 2760 Sym_desc *sdp;
2761 2761 Sym_desc *last_sdp = NULL;
2762 2762 Rel_desc *rsp;
2763 2763 const char *name;
2764 2764
2765 2765 REL_CACHE_TRAVERSE(rcp, idx, rcbp, rsp) {
2766 2766 sdp = rsp->rel_sym;
2767 2767 if ((sdp->sd_isc == NULL) || ((sdp->sd_isc->is_flags &
2768 2768 (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != FLG_IS_INSTRMRG) ||
2769 2769 (sdp->sd_isc->is_osdesc != osp))
2770 2770 continue;
2771 2771
2772 2772 /*
2773 2773 * Remember symbol for use in the third pass. There is no
2774 2774 * reason to save a given symbol more than once, so we take
2775 2775 * advantage of the fact that relocations to a given symbol
2776 2776 * tend to cluster in the list. If this is the same symbol
2777 2777 * we saved last time, don't bother.
2778 2778 */
2779 2779 if (last_sdp != sdp) {
2780 2780 if (aplist_append(sym_alpp, sdp, AL_CNT_STRMRGSYM) ==
2781 2781 NULL)
2782 2782 return (0);
2783 2783 last_sdp = sdp;
2784 2784 }
2785 2785
2786 2786 /* Enter the string into our new string table */
2787 2787 name = strmerge_get_reloc_str(ofl, rsp);
2788 2788 if (st_insert(mstrtab, name) == -1)
2789 2789 return (0);
2790 2790
2791 2791 /*
2792 2792 * If this is an STT_SECTION symbol, then the second pass
2793 2793 * will need to modify this relocation, so hang on to it.
2794 2794 */
2795 2795 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
2796 2796 (aplist_append(rel_alpp, rsp, AL_CNT_STRMRGREL) == NULL))
2797 2797 return (0);
2798 2798 }
2799 2799
2800 2800 return (1);
2801 2801 }
2802 2802
2803 2803 /*
2804 2804 * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2805 2805 * replace them with a single merged/compressed input section.
2806 2806 *
2807 2807 * entry:
2808 2808 * ofl - Output file descriptor
2809 2809 * osp - Output section descriptor
2810 2810 * rel_alpp, sym_alpp, - Address of 2 APlists, to be used
2811 2811 * for internal processing. On the initial call to
2812 2812 * ld_make_strmerge, these list pointers must be NULL.
2813 2813 * The caller is encouraged to pass the same lists back for
2814 2814 * successive calls to this function without freeing
2815 2815 * them in between calls. This causes a single pair of
2816 2816 * memory allocations to be reused multiple times.
2817 2817 *
2818 2818 * exit:
2819 2819 * If section merging is possible, it is done. If no errors are
2820 2820 * encountered, True (1) is returned. On error, S_ERROR.
2821 2821 *
2822 2822 * The contents of rel_alpp and sym_alpp on exit are
2823 2823 * undefined. The caller can free them, or pass them back to a subsequent
2824 2824 * call to this routine, but should not examine their contents.
2825 2825 */
2826 2826 static uintptr_t
2827 2827 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_alpp,
2828 2828 APlist **sym_alpp)
2829 2829 {
2830 2830 Str_tbl *mstrtab; /* string table for string merge secs */
2831 2831 Is_desc *mstrsec; /* Generated string merge section */
2832 2832 Is_desc *isp;
2833 2833 Shdr *mstr_shdr;
2834 2834 Elf_Data *mstr_data;
2835 2835 Sym_desc *sdp;
2836 2836 Rel_desc *rsp;
2837 2837 Aliste idx;
2838 2838 size_t data_size;
2839 2839 int st_setstring_status;
2840 2840 size_t stoff;
2841 2841
2842 2842 /* If string table compression is disabled, there's nothing to do */
2843 2843 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2844 2844 return (1);
2845 2845
2846 2846 /*
2847 2847 * Pass over the mergeable input sections, and if they haven't
2848 2848 * all been discarded, create a string table.
2849 2849 */
2850 2850 mstrtab = NULL;
2851 2851 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2852 2852 if (isp->is_flags & FLG_IS_DISCARD)
2853 2853 continue;
2854 2854
2855 2855 /*
2856 2856 * We have at least one non-discarded section.
2857 2857 * Create a string table descriptor.
2858 2858 */
2859 2859 if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2860 2860 return (S_ERROR);
2861 2861 break;
2862 2862 }
2863 2863
2864 2864 /* If no string table was created, we have no mergeable sections */
2865 2865 if (mstrtab == NULL)
2866 2866 return (1);
2867 2867
2868 2868 /*
2869 2869 * This routine has to make 3 passes:
2870 2870 *
2871 2871 * 1) Examine all relocations, insert strings from relocations
2872 2872 * to the mergeable input sections into the string table.
2873 2873 * 2) Modify the relocation values to be correct for the
2874 2874 * new merged section.
2875 2875 * 3) Modify the symbols used by the relocations to reference
2876 2876 * the new section.
2877 2877 *
2878 2878 * These passes cannot be combined:
2879 2879 * - The string table code works in two passes, and all
2880 2880 * strings have to be loaded in pass one before the
2881 2881 * offset of any strings can be determined.
2882 2882 * - Multiple relocations reference a single symbol, so the
2883 2883 * symbol cannot be modified until all relocations are
2884 2884 * fixed.
2885 2885 *
2886 2886 * The number of relocations related to section merging is usually
2887 2887 * a mere fraction of the overall active and output relocation lists,
2888 2888 * and the number of symbols is usually a fraction of the number
2889 2889 * of related relocations. We therefore build APlists for the
2890 2890 * relocations and symbols in the first pass, and then use those
2891 2891 * lists to accelerate the operation of pass 2 and 3.
2892 2892 *
2893 2893 * Reinitialize the lists to a completely empty state.
2894 2894 */
2895 2895 aplist_reset(*rel_alpp);
2896 2896 aplist_reset(*sym_alpp);
2897 2897
2898 2898 /*
2899 2899 * Pass 1:
2900 2900 *
2901 2901 * Every relocation related to this output section (and the input
2902 2902 * sections that make it up) is found in either the active, or the
2903 2903 * output relocation list, depending on whether the relocation is to
2904 2904 * be processed by this invocation of the linker, or inserted into the
2905 2905 * output object.
2906 2906 *
2907 2907 * Build lists of relocations and symbols that will need modification,
2908 2908 * and insert the strings they reference into the mstrtab string table.
2909 2909 */
2910 2910 if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2911 2911 &ofl->ofl_actrels) == 0)
2912 2912 goto return_s_error;
2913 2913 if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2914 2914 &ofl->ofl_outrels) == 0)
2915 2915 goto return_s_error;
2916 2916
2917 2917 /*
2918 2918 * Get the size of the new input section. Requesting the
2919 2919 * string table size "cooks" the table, and finalizes its contents.
2920 2920 */
2921 2921 data_size = st_getstrtab_sz(mstrtab);
2922 2922
2923 2923 /* Create a new input section to hold the merged strings */
2924 2924 if (new_section_from_template(ofl, isp, data_size,
2925 2925 &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2926 2926 goto return_s_error;
2927 2927 mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2928 2928
2929 2929 /*
2930 2930 * Allocate a data buffer for the new input section.
2931 2931 * Then, associate the buffer with the string table descriptor.
2932 2932 */
2933 2933 if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL)
2934 2934 goto return_s_error;
2935 2935 if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2936 2936 goto return_s_error;
2937 2937
2938 2938 /* Add the new section to the output image */
2939 2939 if (ld_place_section(ofl, mstrsec, NULL, osp->os_identndx, NULL) ==
2940 2940 (Os_desc *)S_ERROR)
2941 2941 goto return_s_error;
2942 2942
2943 2943 /*
2944 2944 * Pass 2:
2945 2945 *
2946 2946 * Revisit the relocation descriptors with STT_SECTION symbols
2947 2947 * that were saved by the first pass. Update each relocation
2948 2948 * record so that the offset it contains is for the new section
2949 2949 * instead of the original.
2950 2950 */
2951 2951 for (APLIST_TRAVERSE(*rel_alpp, idx, rsp)) {
2952 2952 const char *name;
2953 2953
2954 2954 /* Put the string into the merged string table */
2955 2955 name = strmerge_get_reloc_str(ofl, rsp);
2956 2956 st_setstring_status = st_setstring(mstrtab, name, &stoff);
2957 2957 if (st_setstring_status == -1) {
2958 2958 /*
2959 2959 * A failure to insert at this point means that
2960 2960 * something is corrupt. This isn't a resource issue.
2961 2961 */
2962 2962 assert(st_setstring_status != -1);
2963 2963 goto return_s_error;
2964 2964 }
2965 2965
2966 2966 /*
2967 2967 * Alter the relocation to access the string at the
2968 2968 * new offset in our new string table.
2969 2969 *
2970 2970 * For SHT_RELA platforms, it suffices to simply
2971 2971 * update the rel_raddend field of the relocation.
2972 2972 *
2973 2973 * For SHT_REL platforms, the new "addend" value
2974 2974 * needs to be written at the address being relocated.
2975 2975 * However, we can't alter the input sections which
2976 2976 * are mapped readonly, and the output image has not
2977 2977 * been created yet. So, we defer this operation,
2978 2978 * using the rel_raddend field of the relocation
2979 2979 * which is normally 0 on a REL platform, to pass the
2980 2980 * new "addend" value to ld_perform_outreloc() or
2981 2981 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2982 2982 * tells them that this is the case.
2983 2983 */
2984 2984 if ((rsp->rel_flags & FLG_REL_RELA) == 0) /* REL */
2985 2985 rsp->rel_flags |= FLG_REL_NADDEND;
2986 2986 rsp->rel_raddend = (Sxword)stoff;
2987 2987
2988 2988 /*
2989 2989 * Generate a symbol name string for STT_SECTION symbols
2990 2990 * that might reference our merged section. This shows up
2991 2991 * in debug output and helps show how the relocation has
2992 2992 * changed from its original input section to our merged one.
2993 2993 */
2994 2994 if (ld_stt_section_sym_name(mstrsec) == NULL)
2995 2995 goto return_s_error;
2996 2996 }
2997 2997
2998 2998 /*
2999 2999 * Pass 3:
3000 3000 *
3001 3001 * Modify the symbols referenced by the relocation descriptors
3002 3002 * so that they reference the new input section containing the
3003 3003 * merged strings instead of the original input sections.
3004 3004 */
3005 3005 for (APLIST_TRAVERSE(*sym_alpp, idx, sdp)) {
3006 3006 /*
3007 3007 * If we've already processed this symbol, don't do it
3008 3008 * twice. strmerge_pass1() uses a heuristic (relocations to
3009 3009 * the same symbol clump together) to avoid inserting a
3010 3010 * given symbol more than once, but repeat symbols in
3011 3011 * the list can occur.
3012 3012 */
3013 3013 if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
3014 3014 continue;
3015 3015
3016 3016 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
3017 3017 /*
3018 3018 * This is not an STT_SECTION symbol, so its
3019 3019 * value is the offset of the string within the
3020 3020 * input section. Update the address to reflect
3021 3021 * the address in our new merged section.
3022 3022 */
3023 3023 const char *name = sdp->sd_sym->st_value +
3024 3024 (char *)sdp->sd_isc->is_indata->d_buf;
3025 3025
3026 3026 st_setstring_status =
3027 3027 st_setstring(mstrtab, name, &stoff);
3028 3028 if (st_setstring_status == -1) {
3029 3029 /*
3030 3030 * A failure to insert at this point means
3031 3031 * something is corrupt. This isn't a
3032 3032 * resource issue.
3033 3033 */
3034 3034 assert(st_setstring_status != -1);
3035 3035 goto return_s_error;
3036 3036 }
3037 3037
3038 3038 if (ld_sym_copy(sdp) == S_ERROR)
3039 3039 goto return_s_error;
3040 3040 sdp->sd_sym->st_value = (Word)stoff;
3041 3041 }
3042 3042
3043 3043 /* Redirect the symbol to our new merged section */
3044 3044 sdp->sd_isc = mstrsec;
3045 3045 }
3046 3046
3047 3047 /*
3048 3048 * There are no references left to the original input string sections.
3049 3049 * Mark them as discarded so they don't go into the output image.
3050 3050 * At the same time, add up the sizes of the replaced sections.
3051 3051 */
3052 3052 data_size = 0;
3053 3053 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
3054 3054 if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
3055 3055 continue;
3056 3056
3057 3057 data_size += isp->is_indata->d_size;
3058 3058
3059 3059 isp->is_flags |= FLG_IS_DISCARD;
3060 3060 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
3061 3061 }
3062 3062
3063 3063 /* Report how much space we saved in the output section */
3064 3064 DBG_CALL(Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
3065 3065 mstr_data->d_size));
3066 3066
3067 3067 st_destroy(mstrtab);
3068 3068 return (1);
3069 3069
3070 3070 return_s_error:
3071 3071 st_destroy(mstrtab);
3072 3072 return (S_ERROR);
3073 3073 }
3074 3074
3075 3075 /*
3076 3076 * Update a data buffers size. A number of sections have to be created, and
3077 3077 * the sections header contributes to the size of the eventual section. Thus,
3078 3078 * a section may be created, and once all associated sections have been created,
3079 3079 * we return to establish the required section size.
3080 3080 */
3081 3081 inline static void
3082 3082 update_data_size(Os_desc *osp, ulong_t cnt)
3083 3083 {
3084 3084 Is_desc *isec = ld_os_first_isdesc(osp);
3085 3085 Elf_Data *data = isec->is_indata;
3086 3086 Shdr *shdr = osp->os_shdr;
3087 3087 size_t size = cnt * shdr->sh_entsize;
3088 3088
3089 3089 shdr->sh_size = (Xword)size;
3090 3090 data->d_size = size;
3091 3091 }
3092 3092
3093 3093 /*
3094 3094 * The following sections are built after all input file processing and symbol
3095 3095 * validation has been carried out. The order is important (because the
3096 3096 * addition of a section adds a new symbol there is a chicken and egg problem
3097 3097 * of maintaining the appropriate counts). By maintaining a known order the
3098 3098 * individual routines can compensate for later, known, additions.
3099 3099 */
3100 3100 uintptr_t
3101 3101 ld_make_sections(Ofl_desc *ofl)
3102 3102 {
3103 3103 ofl_flag_t flags = ofl->ofl_flags;
3104 3104 Sg_desc *sgp;
3105 3105
3106 3106 /*
3107 3107 * Generate any special sections.
3108 3108 */
3109 3109 if (flags & FLG_OF_ADDVERS)
3110 3110 if (make_comment(ofl) == S_ERROR)
3111 3111 return (S_ERROR);
3112 3112
3113 3113 if (make_interp(ofl) == S_ERROR)
3114 3114 return (S_ERROR);
3115 3115
3116 3116 /*
3117 3117 * Create a capabilities section if required.
3118 3118 */
3119 3119 if (make_cap(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP),
3120 3120 ld_targ.t_id.id_cap) == S_ERROR)
3121 3121 return (S_ERROR);
3122 3122
3123 3123 /*
3124 3124 * Create any init/fini array sections.
3125 3125 */
3126 3126 if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
3127 3127 ofl->ofl_initarray) == S_ERROR)
3128 3128 return (S_ERROR);
3129 3129
3130 3130 if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
3131 3131 ofl->ofl_finiarray) == S_ERROR)
3132 3132 return (S_ERROR);
3133 3133
3134 3134 if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
3135 3135 ofl->ofl_preiarray) == S_ERROR)
3136 3136 return (S_ERROR);
3137 3137
3138 3138 /*
3139 3139 * Make the .plt section. This occurs after any other relocation
3140 3140 * sections are generated (see reloc_init()) to ensure that the
3141 3141 * associated relocation section is after all the other relocation
3142 3142 * sections.
3143 3143 */
3144 3144 if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
3145 3145 if (make_plt(ofl) == S_ERROR)
3146 3146 return (S_ERROR);
3147 3147
3148 3148 /*
3149 3149 * Determine whether any sections or files are not referenced. Under
3150 3150 * -Dunused a diagnostic for any unused components is generated, under
3151 3151 * -zignore the component is removed from the final output.
3152 3152 */
3153 3153 if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
3154 3154 if (ignore_section_processing(ofl) == S_ERROR)
3155 3155 return (S_ERROR);
3156 3156 }
3157 3157
3158 3158 /*
3159 3159 * If we have detected a situation in which previously placed
3160 3160 * output sections may have been discarded, perform the necessary
3161 3161 * readjustment.
3162 3162 */
3163 3163 if (ofl->ofl_flags & FLG_OF_ADJOSCNT)
3164 3164 adjust_os_count(ofl);
3165 3165
3166 3166 /*
3167 3167 * Do any of the output sections contain input sections that
3168 3168 * are candidates for string table merging? For each such case,
3169 3169 * we create a replacement section, insert it, and discard the
3170 3170 * originals.
3171 3171 *
3172 3172 * rel_alpp and sym_alpp are used by ld_make_strmerge()
3173 3173 * for its internal processing. We are responsible for the
3174 3174 * initialization and cleanup, and ld_make_strmerge() handles the rest.
3175 3175 * This allows us to reuse a single pair of memory buffers, allocated
3176 3176 * for this processing, for all the output sections.
3177 3177 */
3178 3178 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
3179 3179 int error_seen = 0;
3180 3180 APlist *rel_alpp = NULL;
3181 3181 APlist *sym_alpp = NULL;
3182 3182 Aliste idx1;
3183 3183
3184 3184 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3185 3185 Os_desc *osp;
3186 3186 Aliste idx2;
3187 3187
3188 3188 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp))
3189 3189 if ((osp->os_mstrisdescs != NULL) &&
3190 3190 (ld_make_strmerge(ofl, osp,
3191 3191 &rel_alpp, &sym_alpp) ==
3192 3192 S_ERROR)) {
3193 3193 error_seen = 1;
3194 3194 break;
3195 3195 }
3196 3196 }
3197 3197 if (rel_alpp != NULL)
3198 3198 libld_free(rel_alpp);
3199 3199 if (sym_alpp != NULL)
3200 3200 libld_free(sym_alpp);
3201 3201 if (error_seen != 0)
3202 3202 return (S_ERROR);
3203 3203 }
3204 3204
3205 3205 /*
3206 3206 * Add any necessary versioning information.
3207 3207 */
3208 3208 if (!(flags & FLG_OF_NOVERSEC)) {
3209 3209 if ((flags & FLG_OF_VERNEED) &&
3210 3210 (make_verneed(ofl) == S_ERROR))
3211 3211 return (S_ERROR);
3212 3212 if ((flags & FLG_OF_VERDEF) &&
3213 3213 (make_verdef(ofl) == S_ERROR))
3214 3214 return (S_ERROR);
3215 3215 if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
3216 3216 ((ofl->ofl_osversym = make_sym_sec(ofl,
3217 3217 MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
3218 3218 ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
3219 3219 return (S_ERROR);
3220 3220 }
3221 3221
3222 3222 /*
3223 3223 * Create a syminfo section if necessary.
3224 3224 */
3225 3225 if (flags & FLG_OF_SYMINFO) {
3226 3226 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
3227 3227 MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
3228 3228 ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
3229 3229 return (S_ERROR);
3230 3230 }
3231 3231
3232 3232 if (flags & FLG_OF_COMREL) {
3233 3233 /*
3234 3234 * If -zcombreloc is enabled then all relocations (except for
3235 3235 * the PLT's) are coalesced into a single relocation section.
3236 3236 */
3237 3237 if (ofl->ofl_reloccnt) {
3238 3238 if (make_reloc(ofl, NULL) == S_ERROR)
3239 3239 return (S_ERROR);
3240 3240 }
3241 3241 } else {
3242 3242 Aliste idx1;
3243 3243
3244 3244 /*
3245 3245 * Create the required output relocation sections. Note, new
3246 3246 * sections may be added to the section list that is being
3247 3247 * traversed. These insertions can move the elements of the
3248 3248 * Alist such that a section descriptor is re-read. Recursion
3249 3249 * is prevented by maintaining a previous section pointer and
3250 3250 * insuring that this pointer isn't re-examined.
3251 3251 */
3252 3252 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3253 3253 Os_desc *osp, *posp = 0;
3254 3254 Aliste idx2;
3255 3255
3256 3256 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3257 3257 if ((osp != posp) && osp->os_szoutrels &&
3258 3258 (osp != ofl->ofl_osplt)) {
3259 3259 if (make_reloc(ofl, osp) == S_ERROR)
3260 3260 return (S_ERROR);
3261 3261 }
3262 3262 posp = osp;
3263 3263 }
3264 3264 }
3265 3265
3266 3266 /*
3267 3267 * If we're not building a combined relocation section, then
3268 3268 * build a .rel[a] section as required.
3269 3269 */
3270 3270 if (ofl->ofl_relocrelsz) {
3271 3271 if (make_reloc(ofl, NULL) == S_ERROR)
3272 3272 return (S_ERROR);
3273 3273 }
3274 3274 }
3275 3275
3276 3276 /*
3277 3277 * The PLT relocations are always in their own section, and we try to
3278 3278 * keep them at the end of the PLT table. We do this to keep the hot
3279 3279 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
3280 3280 */
3281 3281 if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
3282 3282 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
3283 3283 return (S_ERROR);
3284 3284 }
3285 3285
3286 3286 /*
3287 3287 * Finally build the symbol and section header sections.
3288 3288 */
3289 3289 if (flags & FLG_OF_DYNAMIC) {
3290 3290 if (make_dynamic(ofl) == S_ERROR)
3291 3291 return (S_ERROR);
3292 3292
3293 3293 /*
3294 3294 * A number of sections aren't necessary within a relocatable
3295 3295 * object, even if -dy has been used.
3296 3296 */
3297 3297 if (!(flags & FLG_OF_RELOBJ)) {
3298 3298 if (make_hash(ofl) == S_ERROR)
3299 3299 return (S_ERROR);
3300 3300 if (make_dynstr(ofl) == S_ERROR)
3301 3301 return (S_ERROR);
3302 3302 if (make_dynsym(ofl) == S_ERROR)
3303 3303 return (S_ERROR);
3304 3304 if (ld_unwind_make_hdr(ofl) == S_ERROR)
3305 3305 return (S_ERROR);
3306 3306 if (make_dynsort(ofl) == S_ERROR)
3307 3307 return (S_ERROR);
3308 3308 }
3309 3309 }
3310 3310
3311 3311 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
3312 3312 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
3313 3313 /*
3314 3314 * Do we need to make a SHT_SYMTAB_SHNDX section
3315 3315 * for the dynsym. If so - do it now.
3316 3316 */
3317 3317 if (ofl->ofl_osdynsym &&
3318 3318 ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
3319 3319 if (make_dynsym_shndx(ofl) == S_ERROR)
3320 3320 return (S_ERROR);
3321 3321 }
3322 3322
3323 3323 if (make_strtab(ofl) == S_ERROR)
3324 3324 return (S_ERROR);
3325 3325 if (make_symtab(ofl) == S_ERROR)
3326 3326 return (S_ERROR);
3327 3327 } else {
3328 3328 /*
3329 3329 * Do we need to make a SHT_SYMTAB_SHNDX section
3330 3330 * for the dynsym. If so - do it now.
3331 3331 */
3332 3332 if (ofl->ofl_osdynsym &&
3333 3333 ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
3334 3334 if (make_dynsym_shndx(ofl) == S_ERROR)
3335 3335 return (S_ERROR);
3336 3336 }
3337 3337 }
3338 3338
3339 3339 if (make_shstrtab(ofl) == S_ERROR)
3340 3340 return (S_ERROR);
3341 3341
3342 3342 /*
3343 3343 * Now that we've created all output sections, adjust the size of the
3344 3344 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
3345 3345 * the associated symbol table sizes.
3346 3346 */
3347 3347 if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
3348 3348 ulong_t cnt;
3349 3349 Is_desc *isp;
3350 3350 Os_desc *osp;
3351 3351
3352 3352 if (OFL_IS_STATIC_OBJ(ofl))
3353 3353 osp = ofl->ofl_ossymtab;
3354 3354 else
3355 3355 osp = ofl->ofl_osdynsym;
3356 3356
3357 3357 isp = ld_os_first_isdesc(osp);
3358 3358 cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize);
3359 3359
3360 3360 if (ofl->ofl_osversym)
3361 3361 update_data_size(ofl->ofl_osversym, cnt);
3362 3362
3363 3363 if (ofl->ofl_ossyminfo)
3364 3364 update_data_size(ofl->ofl_ossyminfo, cnt);
3365 3365 }
3366 3366
3367 3367 /*
3368 3368 * Now that we've created all output sections, adjust the size of the
3369 3369 * SHT_SUNW_capinfo, which is dependent on the associated symbol table
3370 3370 * size.
3371 3371 */
3372 3372 if (ofl->ofl_oscapinfo) {
3373 3373 ulong_t cnt;
3374 3374
3375 3375 /*
3376 3376 * Symbol capabilities symbols are placed directly after the
3377 3377 * STT_FILE symbol, section symbols, and any register symbols.
3378 3378 * Effectively these are the first of any series of demoted
3379 3379 * (scoped) symbols.
3380 3380 */
3381 3381 if (OFL_IS_STATIC_OBJ(ofl))
3382 3382 cnt = SYMTAB_ALL_CNT(ofl);
3383 3383 else
3384 3384 cnt = DYNSYM_ALL_CNT(ofl);
3385 3385
3386 3386 update_data_size(ofl->ofl_oscapinfo, cnt);
3387 3387 }
3388 3388 return (1);
3389 3389 }
3390 3390
3391 3391 /*
3392 3392 * Build an additional data section - used to back OBJT symbol definitions
3393 3393 * added with a mapfile.
3394 3394 */
3395 3395 Is_desc *
3396 3396 ld_make_data(Ofl_desc *ofl, size_t size)
3397 3397 {
3398 3398 Shdr *shdr;
3399 3399 Elf_Data *data;
3400 3400 Is_desc *isec;
3401 3401
3402 3402 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
3403 3403 &isec, &shdr, &data) == S_ERROR)
3404 3404 return ((Is_desc *)S_ERROR);
3405 3405
3406 3406 data->d_size = size;
3407 3407 shdr->sh_size = (Xword)size;
3408 3408 shdr->sh_flags |= SHF_WRITE;
3409 3409
3410 3410 if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
3411 3411 return ((Is_desc *)S_ERROR);
3412 3412
3413 3413 return (isec);
3414 3414 }
3415 3415
3416 3416 /*
3417 3417 * Build an additional text section - used to back FUNC symbol definitions
3418 3418 * added with a mapfile.
3419 3419 */
3420 3420 Is_desc *
3421 3421 ld_make_text(Ofl_desc *ofl, size_t size)
3422 3422 {
3423 3423 Shdr *shdr;
3424 3424 Elf_Data *data;
3425 3425 Is_desc *isec;
3426 3426
3427 3427 /*
3428 3428 * Insure the size is sufficient to contain the minimum return
3429 3429 * instruction.
3430 3430 */
3431 3431 if (size < ld_targ.t_nf.nf_size)
3432 3432 size = ld_targ.t_nf.nf_size;
3433 3433
3434 3434 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
3435 3435 &isec, &shdr, &data) == S_ERROR)
3436 3436 return ((Is_desc *)S_ERROR);
3437 3437
3438 3438 data->d_size = size;
3439 3439 shdr->sh_size = (Xword)size;
3440 3440 shdr->sh_flags |= SHF_EXECINSTR;
3441 3441
3442 3442 /*
3443 3443 * Fill the buffer with the appropriate return instruction.
3444 3444 * Note that there is no need to swap bytes on a non-native,
3445 3445 * link, as the data being copied is given in bytes.
3446 3446 */
3447 3447 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
3448 3448 return ((Is_desc *)S_ERROR);
3449 3449 (void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
3450 3450 ld_targ.t_nf.nf_size);
3451 3451
3452 3452 /*
3453 3453 * If size was larger than required, and the target supplies
3454 3454 * a fill function, use it to fill the balance. If there is no
3455 3455 * fill function, we accept the 0-fill supplied by libld_calloc().
↓ open down ↓ |
3455 lines elided |
↑ open up ↑ |
3456 3456 */
3457 3457 if ((ld_targ.t_ff.ff_execfill != NULL) && (size > ld_targ.t_nf.nf_size))
3458 3458 ld_targ.t_ff.ff_execfill(data->d_buf, ld_targ.t_nf.nf_size,
3459 3459 size - ld_targ.t_nf.nf_size);
3460 3460
3461 3461 if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
3462 3462 return ((Is_desc *)S_ERROR);
3463 3463
3464 3464 return (isec);
3465 3465 }
3466 +
3467 +void
3468 +ld_comdat_validate(Ofl_desc *ofl, Ifl_desc *ifl)
3469 +{
3470 + int i;
3471 +
3472 + for (i = 0; i < ifl->ifl_shnum; i++) {
3473 + Is_desc *isp = ifl->ifl_isdesc[i];
3474 + int types = 0;
3475 + char buf[1024] = "";
3476 + Group_desc *gr = NULL;
3477 +
3478 + if ((isp == NULL) || (isp->is_flags & FLG_IS_COMDAT) == 0)
3479 + continue;
3480 +
3481 + if (isp->is_shdr->sh_type == SHT_SUNW_COMDAT) {
3482 + types++;
3483 + (void) strlcpy(buf, MSG_ORIG(MSG_STR_SUNW_COMDAT),
3484 + sizeof (buf));
3485 + }
3486 +
3487 + if (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
3488 + MSG_SCN_GNU_LINKONCE_SIZE) == 0) {
3489 + types++;
3490 + if (types > 1)
3491 + (void) strlcat(buf, ", ", sizeof (buf));
3492 + (void) strlcat(buf, MSG_ORIG(MSG_SCN_GNU_LINKONCE),
3493 + sizeof (buf));
3494 + }
3495 +
3496 + if ((isp->is_shdr->sh_flags & SHF_GROUP) &&
3497 + ((gr = ld_get_group(ofl, isp)) != NULL) &&
3498 + (gr->gd_data[0] & GRP_COMDAT)) {
3499 + types++;
3500 + if (types > 1)
3501 + (void) strlcat(buf, ", ", sizeof (buf));
3502 + (void) strlcat(buf, MSG_ORIG(MSG_STR_GROUP),
3503 + sizeof (buf));
3504 + }
3505 +
3506 + if (types > 1)
3507 + ld_eprintf(ofl, ERR_FATAL,
3508 + MSG_INTL(MSG_SCN_MULTICOMDAT), ifl->ifl_name,
3509 + EC_WORD(isp->is_scnndx), isp->is_name, buf);
3510 + }
3511 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX