Print this page
4003 dldump() can't deal with extended sections

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/sgs/librtld/common/dldump.c
          +++ new/usr/src/cmd/sgs/librtld/common/dldump.c
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   *
  26   26   * dldump(3c) creates a new file image from the specified input file.
  27   27   */
  28      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  29   28  
  30   29  #include        <sys/param.h>
  31   30  #include        <sys/procfs.h>
  32   31  #include        <fcntl.h>
  33   32  #include        <stdio.h>
  34   33  #include        <libelf.h>
  35   34  #include        <link.h>
  36   35  #include        <dlfcn.h>
  37   36  #include        <stdlib.h>
  38   37  #include        <string.h>
↓ open down ↓ 112 lines elided ↑ open up ↑
 151  150          Cache           *icache = 0, *_icache, *mcache = 0, *_mcache;
 152  151          Cache           *data_cache = 0, *dyn_cache = 0;
 153  152          Xword           rel_null_no = 0, rel_data_no = 0, rel_func_no = 0;
 154  153          Xword           rel_entsize;
 155  154          Rel             *rel_base = 0, *rel_null, *rel_data, *rel_func;
 156  155          Elf_Scn         *scn;
 157  156          Shdr            *shdr;
 158  157          Elf_Data        *data;
 159  158          Half            endx = 1;
 160  159          int             fd = 0, err, num;
 161      -        size_t          shstr_size = 1;
      160 +        size_t          shstr_size = 1, shndx;
 162  161          Addr            edata;
 163  162          char            *shstr, *_shstr, *ipath = NAME(lmp);
 164  163          prstatus_t      *status = 0, _status;
 165  164          Lm_list         *lml = LIST(lmp);
 166  165          Alist           *nodirect = 0;
 167  166  
 168  167          if (lmp == lml_main.lm_head) {
 169  168                  char    proc[16];
 170  169                  int     pfd;
 171  170  
↓ open down ↓ 130 lines elided ↑ open up ↑
 302  301           * segment, and would therefore have the wrong permissions).
 303  302           */
 304  303          if (status && !data_phdr) {
 305  304                  eprintf(lml, ERR_WARNING, MSG_INTL(MSG_IMG_DATASEG), ipath);
 306  305                  status = 0;
 307  306          }
 308  307  
 309  308          /*
 310  309           * Obtain the input files section header string table.
 311  310           */
 312      -        if ((scn = elf_getscn(ielf, iehdr->e_shstrndx)) == NULL) {
      311 +
      312 +        if (elf_getshdrstrndx(ielf, &shndx) == -1) {
      313 +                eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETSHDRSTRNDX), ipath);
      314 +                cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
      315 +                return (1);
      316 +        }
      317 +        if ((scn = elf_getscn(ielf, shndx)) == NULL) {
 313  318                  eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETSCN), ipath);
 314  319                  cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
 315  320                  return (1);
 316  321          }
 317  322          if ((data = elf_getdata(scn, NULL)) == NULL) {
 318  323                  eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETDATA), ipath);
 319  324                  cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
 320  325                  return (1);
 321  326          }
 322  327          shstr = (char *)data->d_buf;
 323  328  
 324  329          /*
 325  330           * Construct a cache to maintain the input files section information.
 326  331           * Obtain an extra cache element if a heap addition is required.  Also
 327  332           * add an additional entry (marked FLG_C_END) to make the processing of
 328  333           * this cache easier.
 329  334           */
 330      -        num = iehdr->e_shnum;
      335 +
      336 +        if (elf_getshdrnum(ielf, &shndx) == -1) {
      337 +                eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETSHDRNUM), opath);
      338 +                cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
      339 +                return (1);
      340 +        }
      341 +
      342 +        num = shndx;
      343 +
 331  344          if (status)
 332  345                  num++;
 333  346          if ((icache = malloc((num + 1) * sizeof (Cache))) == 0) {
 334  347                  cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
 335  348                  return (1);
 336  349          }
 337  350          icache[num].c_flags = FLG_C_END;
 338  351  
 339  352          _icache = icache;
 340  353          _icache++;
↓ open down ↓ 72 lines elided ↑ open up ↑
 413  426                   * indicate that it will be added following the last data
 414  427                   * section.
 415  428                   */
 416  429                  if (shdr->sh_addr && ((shdr->sh_addr + shdr->sh_size) ==
 417  430                      (data_phdr->p_vaddr + data_phdr->p_memsz))) {
 418  431                          data_cache = _icache;
 419  432  
 420  433                          if (status) {
 421  434                                  _icache++;
 422  435                                  _icache->c_name =
 423      -                                        (char *)MSG_ORIG(MSG_SCN_HEAP);
      436 +                                    (char *)MSG_ORIG(MSG_SCN_HEAP);
 424  437                                  _icache->c_flags = FLG_C_HEAP;
 425  438  
 426  439                                  _icache->c_scn = 0;
 427  440                                  _icache->c_shdr = 0;
 428  441                                  _icache->c_data = 0;
 429  442                                  _icache->c_info = 0;
 430  443  
 431  444                                  shstr_size += strlen(_icache->c_name) + 1;
 432  445                          }
 433  446                  }
↓ open down ↓ 183 lines elided ↑ open up ↑
 617  630                           * this section header table.
 618  631                           */
 619  632                          *data = *_icache->c_data;
 620  633  
 621  634                          data->d_buf = (void *)shstr;
 622  635                          data->d_size = shstr_size;
 623  636  
 624  637                          _icache->c_info = shstr;
 625  638  
 626  639                          /* LINTED */
 627      -                        oehdr->e_shstrndx = (Half)elf_ndxscn(scn);
      640 +                        if (elf_ndxscn(scn) >= SHN_LORESERVE) {
      641 +                                Elf_Scn *_scn;
      642 +                                Shdr    *shdr0;
      643 +
      644 +                                /*
      645 +                                 * libelf deals with e_shnum for us, but we
      646 +                                 * need to deal with e_shstrndx ourselves.
      647 +                                 */
      648 +                                oehdr->e_shstrndx = SHN_XINDEX;
      649 +                                if ((_scn = elf_getscn(oelf, 0)) == NULL) {
      650 +                                        eprintf(lml, ERR_ELF,
      651 +                                            MSG_ORIG(MSG_ELF_GETSCN), opath);
      652 +                                        cleanup(ielf, oelf, melf, icache,
      653 +                                            mcache, fd, opath);
      654 +                                        return (1);
      655 +                                }
      656 +                                shdr0 = elf_getshdr(_scn);
      657 +                                shdr0->sh_link = elf_ndxscn(scn);
      658 +                        } else {
      659 +                                oehdr->e_shstrndx = (Half)elf_ndxscn(scn);
      660 +                        }
 628  661  
 629  662                  } else if (_icache->c_flags == FLG_C_HEAP) {
 630  663                          /*
 631  664                           * Assign the heap to the appropriate memory offset.
 632  665                           */
 633  666                          data->d_buf = status->pr_brkbase;
 634  667                          data->d_type = ELF_T_BYTE;
 635  668                          data->d_size = (size_t)status->pr_brksize;
 636  669                          data->d_off = 0;
 637  670                          data->d_align = 1;
↓ open down ↓ 82 lines elided ↑ open up ↑
 720  753                  eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_BEGIN), opath);
 721  754                  cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
 722  755                  return (1);
 723  756          }
 724  757          if ((mehdr = elf_getehdr(melf)) == NULL) {
 725  758                  eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETEHDR), opath);
 726  759                  cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
 727  760                  return (1);
 728  761          }
 729  762  
      763 +        if (elf_getshdrnum(melf, &shndx) == -1) {
      764 +                eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETSHDRNUM), opath);
      765 +                cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
      766 +                return (1);
      767 +        }
      768 +
 730  769          /*
 731  770           * Construct a cache to maintain the memory files section information.
 732  771           */
 733      -        if ((mcache = malloc(mehdr->e_shnum * sizeof (Cache))) == 0) {
      772 +        if ((mcache = malloc(shndx * sizeof (Cache))) == 0) {
 734  773                  cleanup(ielf, oelf, melf, icache, mcache, fd, opath);
 735  774                  return (1);
 736  775          }
 737  776          _mcache = mcache;
 738  777          _mcache++;
 739  778  
 740  779          for (scn = 0; scn = elf_nextscn(melf, scn); _mcache++) {
 741  780  
 742  781                  if ((_mcache->c_shdr = elf_getshdr(scn)) == NULL) {
 743  782                          eprintf(lml, ERR_ELF, MSG_ORIG(MSG_ELF_GETSHDR), opath);
↓ open down ↓ 101 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX