1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #define ELF_TARGET_AMD64 31 32 #include <stdio.h> 33 #include <memory.h> 34 #include <debug.h> 35 #include "msg.h" 36 #include "_libld.h" 37 38 39 /* 40 * Types of segment index. 41 */ 42 typedef enum { 43 LD_PHDR, 44 LD_INTERP, 45 LD_SUNWCAP, 46 LD_TEXT, 47 LD_DATA, 48 LD_BSS, 49 #if defined(_ELF64) 50 LD_LRODATA, /* (amd64-only) */ 51 LD_LDATA, /* (amd64-only) */ 52 #endif 53 LD_DYN, 54 LD_DTRACE, 55 LD_TLS, 56 LD_UNWIND, 57 LD_NOTE, 58 LD_EXTRA, 59 LD_NUM 60 } Segment_ndx; 61 62 /* 63 * The loader uses a `segment descriptor' list to describe the output 64 * segments it can potentially create. This list is initially seeded 65 * using the templates contained in the sg_desc[] array below. Additional 66 * segments may be added using a map file. 67 * 68 * The entries in sg_desc[] must be put in the order defined by the 69 * Segment_ndx enum, such that a given LD_XXX value can serve as 70 * an index into sg_desc[] for the corresponding descriptor. 71 * 72 * The entries in sg_desc[] are initialized using the SG_DESC_INIT macro 73 * for two reasons: 74 * 75 * 1) The first field of the Sg_desc struct is a program header 76 * entry. ELF32_Phdr and ELF64_Phdr have the same fields, 77 * but their order is different. Use of a macro allows us 78 * to handle this transparently. 79 * 2) Most of the fields in the Sg_desc entries are set to 0. 80 * Use of a macro allows us to hide the clutter. 81 */ 82 #ifdef _ELF64 83 #define SG_DESC_INIT(p_type, p_flags, sg_name, sg_flags) \ 84 { { p_type, p_flags, 0, 0, 0, 0, 0, 0}, \ 85 sg_name, 0, 0, NULL, NULL, sg_flags, NULL, 0, 0} 86 #else 87 #define SG_DESC_INIT(p_type, p_flags, sg_name, sg_flags) \ 88 { { p_type, 0, 0, 0, 0, 0, p_flags, 0}, \ 89 sg_name, 0, 0, NULL, NULL, sg_flags, NULL, 0, 0} 90 #endif 91 92 static const Sg_desc sg_desc[LD_NUM] = { 93 /* LD_PHDR */ 94 SG_DESC_INIT(PT_PHDR, PF_R + PF_X, MSG_ORIG(MSG_ENT_PHDR), 95 (FLG_SG_TYPE | FLG_SG_FLAGS)), 96 97 /* LD_INTERP */ 98 SG_DESC_INIT(PT_INTERP, PF_R, MSG_ORIG(MSG_ENT_INTERP), 99 (FLG_SG_TYPE | FLG_SG_FLAGS)), 100 101 /* LD_SUNWCAP */ 102 SG_DESC_INIT(PT_SUNWCAP, PF_R, MSG_ORIG(MSG_ENT_SUNWCAP), 103 (FLG_SG_TYPE | FLG_SG_FLAGS)), 104 105 /* LD_TEXT */ 106 SG_DESC_INIT(PT_LOAD, PF_R + PF_X, MSG_ORIG(MSG_ENT_TEXT), 107 (FLG_SG_TYPE | FLG_SG_FLAGS)), 108 109 /* LD_DATA */ 110 SG_DESC_INIT(PT_LOAD, 0, MSG_ORIG(MSG_ENT_DATA), 111 (FLG_SG_TYPE | FLG_SG_FLAGS)), 112 113 /* LD_BSS */ 114 SG_DESC_INIT(PT_LOAD, 0, MSG_ORIG(MSG_ENT_BSS), 115 (FLG_SG_TYPE | FLG_SG_FLAGS | FLG_SG_DISABLED)), 116 117 #if defined(_ELF64) 118 /* LD_LRODATA (amd64-only) */ 119 SG_DESC_INIT(PT_LOAD, PF_R, MSG_ORIG(MSG_ENT_LRODATA), 120 (FLG_SG_TYPE | FLG_SG_FLAGS)), 121 122 /* LD_LDATA (amd64-only) */ 123 SG_DESC_INIT(PT_LOAD, 0, MSG_ORIG(MSG_ENT_LDATA), 124 (FLG_SG_TYPE | FLG_SG_FLAGS)), 125 #endif 126 127 /* LD_DYN */ 128 SG_DESC_INIT(PT_DYNAMIC, 0, MSG_ORIG(MSG_ENT_DYNAMIC), 129 (FLG_SG_TYPE | FLG_SG_FLAGS)), 130 131 /* LD_DTRACE */ 132 SG_DESC_INIT(PT_SUNWDTRACE, 0, 133 MSG_ORIG(MSG_ENT_DTRACE), (FLG_SG_TYPE | FLG_SG_FLAGS)), 134 135 /* LD_TLS */ 136 SG_DESC_INIT(PT_TLS, PF_R, MSG_ORIG(MSG_ENT_TLS), 137 (FLG_SG_TYPE | FLG_SG_FLAGS)), 138 139 /* LD_UNWIND */ 140 SG_DESC_INIT(PT_SUNW_UNWIND, PF_R, MSG_ORIG(MSG_ENT_UNWIND), 141 (FLG_SG_TYPE | FLG_SG_FLAGS)), 142 143 /* LD_NOTE */ 144 SG_DESC_INIT(PT_NOTE, 0, MSG_ORIG(MSG_ENT_NOTE), FLG_SG_TYPE), 145 146 /* LD_EXTRA */ 147 SG_DESC_INIT(PT_NULL, 0, MSG_ORIG(MSG_STR_EMPTY), FLG_SG_TYPE) 148 }; 149 150 151 152 /* 153 * The input processing of the loader involves matching the sections of its 154 * input files to an `entrance descriptor definition'. The entrance criteria 155 * is different for either a static or dynamic linkage, and may even be 156 * modified further using a map file. Each entrance criteria is associated 157 * with a segment descriptor, thus a mapping of input sections to output 158 * segments is maintained. 159 * 160 * Note the trick used for the ec_segment field, which is supposed to 161 * be a pointer to a segment descriptor. We initialize this with the 162 * index of the descriptor, and then turn it into an actual pointer 163 * at runtime, once memory has been allocated and the templates copied. 164 */ 165 static const Ent_desc ent_desc[] = { 166 {{NULL, NULL}, NULL, SHT_NOTE, 0, 0, 167 (Sg_desc *)LD_NOTE, 0, FALSE}, 168 169 #if defined(_ELF64) /* (amd64-only) */ 170 {{NULL, NULL}, MSG_ORIG(MSG_SCN_LRODATA), NULL, 171 SHF_ALLOC + SHF_AMD64_LARGE, SHF_ALLOC + SHF_AMD64_LARGE, 172 (Sg_desc *)LD_LRODATA, 0, FALSE}, 173 #endif 174 175 {{NULL, NULL}, NULL, NULL, 176 SHF_ALLOC + SHF_WRITE, SHF_ALLOC, 177 (Sg_desc *)LD_TEXT, 0, FALSE}, 178 179 {{NULL, NULL}, NULL, SHT_NOBITS, 180 SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE, 181 (Sg_desc *)LD_BSS, 0, FALSE}, 182 183 #if defined(_ELF64) /* (amd64-only) */ 184 {{NULL, NULL}, NULL, SHT_NOBITS, 185 SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, 186 SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, 187 (Sg_desc *)LD_DATA, 0, FALSE}, 188 189 {{NULL, NULL}, NULL, NULL, 190 SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, 191 SHF_ALLOC + SHF_WRITE + SHF_AMD64_LARGE, 192 (Sg_desc *)LD_LDATA, 0, FALSE}, 193 #endif 194 195 {{NULL, NULL}, NULL, NULL, 196 SHF_ALLOC + SHF_WRITE, SHF_ALLOC + SHF_WRITE, 197 (Sg_desc *)LD_DATA, 0, FALSE}, 198 199 {{NULL, NULL}, NULL, 0, 0, 0, 200 (Sg_desc *)LD_EXTRA, 0, FALSE} 201 }; 202 203 /* 204 * Initialize new entrance and segment descriptors and add them as lists to 205 * the output file descriptor. 206 */ 207 uintptr_t 208 ld_ent_setup(Ofl_desc *ofl, Xword segalign) 209 { 210 Ent_desc *enp; 211 Sg_desc *sgp; 212 size_t idx; 213 214 /* 215 * Initialize the elf library. 216 */ 217 if (elf_version(EV_CURRENT) == EV_NONE) { 218 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ELF_LIBELF), 219 EV_CURRENT); 220 return (S_ERROR); 221 } 222 223 /* 224 * Initialize internal Global Symbol Table AVL tree 225 */ 226 avl_create(&ofl->ofl_symavl, &ld_sym_avl_comp, sizeof (Sym_avlnode), 227 SGSOFFSETOF(Sym_avlnode, sav_node)); 228 229 /* 230 * Allocate and initialize writable copies of both the entrance and 231 * segment descriptors. 232 * 233 * Note that on non-amd64 targets, this allocates a few more 234 * elements than are needed. For now, we are willing to overallocate 235 * a small amount to simplify the code. 236 */ 237 if ((sgp = libld_malloc(sizeof (sg_desc))) == 0) 238 return (S_ERROR); 239 (void) memcpy(sgp, sg_desc, sizeof (sg_desc)); 240 if ((enp = libld_malloc(sizeof (ent_desc))) == 0) 241 return (S_ERROR); 242 (void) memcpy(enp, ent_desc, sizeof (ent_desc)); 243 244 /* 245 * The data segment permissions can differ: 246 * 247 * - Architecural/ABI per-platform differences 248 * - Whether the object is built statically or dynamically 249 * 250 * Those segments so affected have their program header flags 251 * set here at runtime, rather than in the sg_desc templates above. 252 */ 253 sgp[LD_DATA].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm; 254 sgp[LD_BSS].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm; 255 sgp[LD_DYN].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm; 256 sgp[LD_DTRACE].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm; 257 #if defined(_ELF64) 258 sgp[LD_LDATA].sg_phdr.p_flags = ld_targ.t_m.m_dataseg_perm; 259 sgp[LD_DTRACE].sg_phdr.p_flags |= PF_X; 260 #endif 261 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) == 0) 262 sgp[LD_DATA].sg_phdr.p_flags |= PF_X; 263 264 /* 265 * Traverse the new entrance descriptor list converting the segment 266 * pointer entries to the absolute address within the new segment 267 * descriptor list. Add each entrance descriptor to the output file 268 * list. 269 */ 270 for (idx = 0; idx < (sizeof (ent_desc) / sizeof (ent_desc[0])); 271 idx++, enp++) { 272 #if defined(_ELF64) 273 /* Don't use the amd64 entry conditions for non-amd64 targets */ 274 if ((enp->ec_attrmask & SHF_AMD64_LARGE) && 275 (ld_targ.t_m.m_mach != EM_AMD64)) 276 continue; 277 #endif 278 enp->ec_segment = &sgp[(long)enp->ec_segment]; 279 if ((list_appendc(&ofl->ofl_ents, enp)) == 0) 280 return (S_ERROR); 281 } 282 283 /* 284 * Traverse the new segment descriptor list adding each entry to the 285 * segment descriptor list. For each loadable segment initialize 286 * a default alignment (ld(1) and ld.so.1 initialize this differently). 287 */ 288 for (idx = 0; idx < LD_NUM; idx++, sgp++) { 289 Phdr *phdr = &(sgp->sg_phdr); 290 291 #if defined(_ELF64) 292 /* Ignore amd64 segment templates for non-amd64 targets */ 293 switch (idx) { 294 case LD_LRODATA: 295 case LD_LDATA: 296 if ((ld_targ.t_m.m_mach != EM_AMD64)) 297 continue; 298 } 299 #endif 300 301 if ((list_appendc(&ofl->ofl_segs, sgp)) == 0) 302 return (S_ERROR); 303 if (phdr->p_type == PT_LOAD) 304 phdr->p_align = segalign; 305 } 306 307 return (1); 308 } 309