[BACK]Return to mdreloc.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / libexec / ld.elf_so / arch / sh3

Annotation of src/libexec/ld.elf_so/arch/sh3/mdreloc.c, Revision 1.16

1.16    ! marcus      1: /*     $NetBSD: mdreloc.c,v 1.15 2003/06/30 00:50:46 marcus Exp $      */
1.10      junyoung    2:
1.1       mycroft     3: #include <sys/types.h>
                      4: #include <sys/stat.h>
                      5:
                      6: #include "debug.h"
                      7: #include "rtld.h"
1.11      mycroft     8:
                      9: void _rtld_bind_start(void);
1.1       mycroft    10:
                     11: void
                     12: _rtld_setup_pltgot(const Obj_Entry *obj)
                     13: {
                     14:        obj->pltgot[1] = (Elf_Addr) obj;
                     15:        obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
                     16: }
1.2       mycroft    17:
1.15      marcus     18: void
                     19: _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
                     20: {
                     21:        const Elf_Rela *rela = 0, *relalim;
                     22:        Elf_Addr relasz = 0;
                     23:        Elf_Addr *where;
                     24:
                     25:        for (; dynp->d_tag != DT_NULL; dynp++) {
                     26:                switch (dynp->d_tag) {
                     27:                case DT_RELA:
                     28:                        rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
                     29:                        break;
                     30:                case DT_RELASZ:
                     31:                        relasz = dynp->d_un.d_val;
                     32:                        break;
                     33:                }
                     34:        }
                     35:        relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
                     36:        for (; rela < relalim; rela++) {
                     37:                where = (Elf_Addr *)(relocbase + rela->r_offset);
                     38:                *where = (Elf_Addr)(relocbase + rela->r_addend);
                     39:        }
                     40: }
                     41:
1.2       mycroft    42: int
1.13      mycroft    43: _rtld_relocate_nonplt_objects(obj)
1.7       mycroft    44:        const Obj_Entry *obj;
1.2       mycroft    45: {
1.3       mycroft    46:        const Elf_Rela *rela;
1.2       mycroft    47:
1.3       mycroft    48:        for (rela = obj->rela; rela < obj->relalim; rela++) {
                     49:                Elf_Addr        *where;
                     50:                const Elf_Sym   *def;
                     51:                const Obj_Entry *defobj;
                     52:                Elf_Addr         tmp;
1.4       mycroft    53:                unsigned long    symnum;
1.3       mycroft    54:
                     55:                where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
1.4       mycroft    56:                symnum = ELF_R_SYM(rela->r_info);
1.3       mycroft    57:
                     58:                switch (ELF_R_TYPE(rela->r_info)) {
                     59:                case R_TYPE(NONE):
                     60:                        break;
1.2       mycroft    61:
                     62: #if 1 /* XXX should not occur */
1.3       mycroft    63:                case R_TYPE(GOT32):
1.4       mycroft    64:                        def = _rtld_find_symdef(symnum, obj, &defobj, false);
1.3       mycroft    65:                        if (def == NULL)
                     66:                                return -1;
                     67:
                     68:                        tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
                     69:                            rela->r_addend);
                     70:                        if (*where != tmp)
                     71:                                *where = tmp;
1.12      mycroft    72:                        rdbg(("GOT32 %s in %s --> %p in %s",
1.5       mycroft    73:                            obj->strtab + obj->symtab[symnum].st_name,
                     74:                            obj->path, (void *)*where, defobj->path));
1.3       mycroft    75:                        break;
                     76:
                     77:                case R_TYPE(REL32):
1.4       mycroft    78:                        def = _rtld_find_symdef(symnum, obj, &defobj, false);
1.3       mycroft    79:                        if (def == NULL)
                     80:                                return -1;
                     81:
1.16    ! marcus     82:                        tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
1.3       mycroft    83:                            rela->r_addend) - (Elf_Addr)where;
1.16    ! marcus     84:                        if (*where != tmp)
        !            85:                                *where = tmp;
1.12      mycroft    86:                        rdbg(("PC32 %s in %s --> %p in %s",
1.5       mycroft    87:                            obj->strtab + obj->symtab[symnum].st_name,
                     88:                            obj->path, (void *)*where, defobj->path));
1.3       mycroft    89:                        break;
1.2       mycroft    90: #endif
                     91:
1.3       mycroft    92:                case R_TYPE(DIR32):
1.4       mycroft    93:                        def = _rtld_find_symdef(symnum, obj, &defobj, false);
1.3       mycroft    94:                        if (def == NULL)
                     95:                                return -1;
                     96:
1.16    ! marcus     97:                        tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
1.3       mycroft    98:                            rela->r_addend);
1.16    ! marcus     99:                        if (*where != tmp)
        !           100:                                *where = tmp;
1.12      mycroft   101:                        rdbg(("32 %s in %s --> %p in %s",
1.5       mycroft   102:                            obj->strtab + obj->symtab[symnum].st_name,
                    103:                            obj->path, (void *)*where, defobj->path));
1.3       mycroft   104:                        break;
                    105:
                    106:                case R_TYPE(GLOB_DAT):
1.4       mycroft   107:                        def = _rtld_find_symdef(symnum, obj, &defobj, false);
1.3       mycroft   108:                        if (def == NULL)
                    109:                                return -1;
                    110:
                    111:                        tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
                    112:                            rela->r_addend;
                    113:                        if (*where != tmp)
                    114:                                *where = tmp;
1.12      mycroft   115:                        rdbg(("GLOB_DAT %s in %s --> %p in %s",
1.5       mycroft   116:                            obj->strtab + obj->symtab[symnum].st_name,
                    117:                            obj->path, (void *)*where, defobj->path));
1.3       mycroft   118:                        break;
                    119:
                    120:                case R_TYPE(RELATIVE):
                    121:                        if (rela->r_addend)
                    122:                                *where = (Elf_Addr)obj->relocbase + rela->r_addend;
                    123:                        else
                    124:                                *where += (Elf_Addr)obj->relocbase;
1.12      mycroft   125:                        rdbg(("RELATIVE in %s --> %p", obj->path,
1.3       mycroft   126:                            (void *)*where));
                    127:                        break;
                    128:
                    129:                case R_TYPE(COPY):
                    130:                        /*
                    131:                         * These are deferred until all other relocations have
                    132:                         * been done.  All we do here is make sure that the
                    133:                         * COPY relocation is not in a shared library.  They
                    134:                         * are allowed only in executable files.
                    135:                         */
1.8       mycroft   136:                        if (obj->isdynamic) {
1.3       mycroft   137:                                _rtld_error(
1.2       mycroft   138:                        "%s: Unexpected R_COPY relocation in shared library",
1.3       mycroft   139:                                    obj->path);
                    140:                                return -1;
                    141:                        }
1.12      mycroft   142:                        rdbg(("COPY (avoid in main)"));
1.3       mycroft   143:                        break;
                    144:
                    145:                default:
1.12      mycroft   146:                        rdbg(("sym = %lu, type = %lu, offset = %p, "
1.3       mycroft   147:                            "addend = %p, contents = %p, symbol = %s",
1.4       mycroft   148:                            symnum, (u_long)ELF_R_TYPE(rela->r_info),
1.3       mycroft   149:                            (void *)rela->r_offset, (void *)rela->r_addend,
                    150:                            (void *)*where,
1.4       mycroft   151:                            obj->strtab + obj->symtab[symnum].st_name));
1.3       mycroft   152:                        _rtld_error("%s: Unsupported relocation type %ld "
                    153:                            "in non-PLT relocations\n",
                    154:                            obj->path, (u_long) ELF_R_TYPE(rela->r_info));
1.2       mycroft   155:                        return -1;
                    156:                }
                    157:        }
                    158:        return 0;
                    159: }
1.6       mycroft   160:
                    161: int
1.12      mycroft   162: _rtld_relocate_plt_lazy(obj)
1.7       mycroft   163:        const Obj_Entry *obj;
1.6       mycroft   164: {
                    165:        const Elf_Rela *rela;
                    166:
1.14      mycroft   167:        if (!obj->relocbase)
1.6       mycroft   168:                return 0;
                    169:
                    170:        for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
                    171:                Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
                    172:
                    173:                assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
                    174:
                    175:                /* Just relocate the GOT slots pointing into the PLT */
                    176:                *where += (Elf_Addr)obj->relocbase;
1.12      mycroft   177:                rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
1.6       mycroft   178:        }
                    179:
                    180:        return 0;
1.15      marcus    181: }
                    182:
                    183: caddr_t
                    184: _rtld_bind(obj, reloff)
                    185:        const Obj_Entry *obj;
                    186:        Elf_Word reloff;
                    187: {
                    188:        const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
                    189:        Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
                    190:        Elf_Addr new_value;
                    191:        const Elf_Sym  *def;
                    192:        const Obj_Entry *defobj;
                    193:
                    194:        assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
                    195:
                    196:        def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
                    197:        if (def == NULL)
                    198:                _rtld_die();
                    199:
                    200:        new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
                    201:        rdbg(("bind now/fixup in %s --> old=%p new=%p",
                    202:            defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
                    203:        if (*where != new_value)
                    204:                *where = new_value;
                    205:
                    206:        return (caddr_t)new_value;
1.6       mycroft   207: }
                    208:
                    209: int
1.12      mycroft   210: _rtld_relocate_plt_object(obj, rela, addrp)
1.7       mycroft   211:        const Obj_Entry *obj;
1.6       mycroft   212:        const Elf_Rela *rela;
                    213:        caddr_t *addrp;
                    214: {
                    215:        Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
                    216:        Elf_Addr new_value;
                    217:        const Elf_Sym  *def;
                    218:        const Obj_Entry *defobj;
                    219:
                    220:        assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
                    221:
                    222:        def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
                    223:        if (def == NULL)
                    224:                return -1;
                    225:
                    226:        new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
                    227:            rela->r_addend);
1.12      mycroft   228:        rdbg(("bind now/fixup in %s --> old=%p new=%p",
1.6       mycroft   229:            defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
                    230:        if (*where != new_value)
                    231:                *where = new_value;
                    232:
                    233:        *addrp = (caddr_t)new_value;
                    234:        return 0;
                    235: }

CVSweb <webmaster@jp.NetBSD.org>