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