1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Routines for retrieving CTF data from a .SUNW_ctf ELF section
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <gelf.h>
#include <strings.h>
#include <sys/types.h>
#include "ctftools.h"
#include "memory.h"
#include "symbol.h"
typedef int read_cb_f(tdata_t *, char *, void *);
/*
* Return the source types that the object was generated from.
*/
source_types_t
built_source_types(Elf *elf, char const *file)
{
source_types_t types = SOURCE_NONE;
symit_data_t *si;
if ((si = symit_new(elf, file)) == NULL)
return (SOURCE_NONE);
while (symit_next(si, STT_FILE) != NULL) {
char *name = symit_name(si);
size_t len = strlen(name);
if (len < 2 || name[len - 2] != '.') {
types |= SOURCE_UNKNOWN;
continue;
}
switch (name[len - 1]) {
case 'c':
types |= SOURCE_C;
break;
case 'h':
/* ignore */
break;
case 's':
case 'S':
types |= SOURCE_S;
break;
default:
types |= SOURCE_UNKNOWN;
}
}
symit_free(si);
return (types);
}
static int
read_file(Elf *elf, char *file, char *label, read_cb_f *func, void *arg,
int require_ctf)
{
Elf_Scn *ctfscn;
Elf_Data *ctfdata = NULL;
symit_data_t *si = NULL;
int ctfscnidx;
tdata_t *td;
if ((ctfscnidx = findelfsecidx(elf, file, ".SUNW_ctf")) < 0) {
if (require_ctf &&
(built_source_types(elf, file) & SOURCE_C)) {
terminate("Input file %s was partially built from "
"C sources, but no CTF data was present\n", file);
}
return (0);
}
if ((ctfscn = elf_getscn(elf, ctfscnidx)) == NULL ||
(ctfdata = elf_getdata(ctfscn, NULL)) == NULL)
elfterminate(file, "Cannot read CTF section");
/* Reconstruction of type tree */
if ((si = symit_new(elf, file)) == NULL) {
warning("%s has no symbol table - skipping", file);
return (0);
}
td = ctf_load(file, ctfdata->d_buf, ctfdata->d_size, si, label);
tdata_build_hashes(td);
symit_free(si);
if (td != NULL) {
if (func(td, file, arg) < 0)
return (-1);
else
return (1);
}
return (0);
}
static int
read_archive(int fd, Elf *elf, char *file, char *label, read_cb_f *func,
void *arg, int require_ctf)
{
Elf *melf;
Elf_Cmd cmd = ELF_C_READ;
Elf_Arhdr *arh;
int secnum = 1, found = 0;
while ((melf = elf_begin(fd, cmd, elf)) != NULL) {
int rc = 0;
if ((arh = elf_getarhdr(melf)) == NULL) {
elfterminate(file, "Can't get archive header for "
"member %d", secnum);
}
/* skip special sections - their names begin with "/" */
if (*arh->ar_name != '/') {
size_t memlen = strlen(file) + 1 +
strlen(arh->ar_name) + 1 + 1;
char *memname = xmalloc(memlen);
snprintf(memname, memlen, "%s(%s)", file, arh->ar_name);
switch (elf_kind(melf)) {
case ELF_K_AR:
rc = read_archive(fd, melf, memname, label,
func, arg, require_ctf);
break;
case ELF_K_ELF:
rc = read_file(melf, memname, label,
func, arg, require_ctf);
break;
default:
terminate("%s: Unknown elf kind %d\n",
memname, elf_kind(melf));
}
free(memname);
}
cmd = elf_next(melf);
(void) elf_end(melf);
secnum++;
if (rc < 0)
return (rc);
else
found += rc;
}
return (found);
}
static int
read_ctf_common(char *file, char *label, read_cb_f *func, void *arg,
int require_ctf)
{
Elf *elf;
int found = 0;
int fd;
debug(3, "Reading %s (label %s)\n", file, (label ? label : "NONE"));
(void) elf_version(EV_CURRENT);
if ((fd = open(file, O_RDONLY)) < 0)
terminate("%s: Cannot open for reading", file);
if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
elfterminate(file, "Cannot read");
switch (elf_kind(elf)) {
case ELF_K_AR:
found = read_archive(fd, elf, file, label,
func, arg, require_ctf);
break;
case ELF_K_ELF:
found = read_file(elf, file, label,
func, arg, require_ctf);
break;
default:
terminate("%s: Unknown elf kind %d\n", file, elf_kind(elf));
}
(void) elf_end(elf);
(void) close(fd);
return (found);
}
/*ARGSUSED*/
int
read_ctf_save_cb(tdata_t *td, char *name __unused, void *retp)
{
tdata_t **tdp = retp;
*tdp = td;
return (1);
}
int
read_ctf(char **files, int n, char *label, read_cb_f *func, void *private,
int require_ctf)
{
int found;
int i, rc;
for (i = 0, found = 0; i < n; i++) {
if ((rc = read_ctf_common(files[i], label, func,
private, require_ctf)) < 0)
return (rc);
found += rc;
}
return (found);
}
static int
count_archive(int fd, Elf *elf, char *file)
{
Elf *melf;
Elf_Cmd cmd = ELF_C_READ;
Elf_Arhdr *arh;
int nfiles = 0, err = 0;
while ((melf = elf_begin(fd, cmd, elf)) != NULL) {
if ((arh = elf_getarhdr(melf)) == NULL) {
warning("Can't process input archive %s\n",
file);
err++;
}
if (*arh->ar_name != '/')
nfiles++;
cmd = elf_next(melf);
(void) elf_end(melf);
}
if (err > 0)
return (-1);
return (nfiles);
}
int
count_files(char **files, int n)
{
int nfiles = 0, err = 0;
Elf *elf;
int fd, rc, i;
(void) elf_version(EV_CURRENT);
for (i = 0; i < n; i++) {
char *file = files[i];
if ((fd = open(file, O_RDONLY)) < 0) {
warning("Can't read input file %s", file);
err++;
continue;
}
if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
warning("Can't open input file %s: %s\n", file,
elf_errmsg(-1));
err++;
(void) close(fd);
continue;
}
switch (elf_kind(elf)) {
case ELF_K_AR:
if ((rc = count_archive(fd, elf, file)) < 0)
err++;
else
nfiles += rc;
break;
case ELF_K_ELF:
nfiles++;
break;
default:
warning("Input file %s is corrupt\n", file);
err++;
}
(void) elf_end(elf);
(void) close(fd);
}
if (err > 0)
return (-1);
debug(2, "Found %d files in %d input files\n", nfiles, n);
return (nfiles);
}
struct symit_data {
GElf_Shdr si_shdr;
Elf_Data *si_symd;
Elf_Data *si_strd;
GElf_Sym si_cursym;
char *si_curname;
char *si_curfile;
int si_nument;
int si_next;
};
symit_data_t *
symit_new(Elf *elf, const char *file)
{
symit_data_t *si;
Elf_Scn *scn;
int symtabidx;
if ((symtabidx = findelfsecidx(elf, file, ".symtab")) < 0)
return (NULL);
si = xcalloc(sizeof (symit_data_t));
if ((scn = elf_getscn(elf, symtabidx)) == NULL ||
gelf_getshdr(scn, &si->si_shdr) == NULL ||
(si->si_symd = elf_getdata(scn, NULL)) == NULL)
elfterminate(file, "Cannot read .symtab");
if ((scn = elf_getscn(elf, si->si_shdr.sh_link)) == NULL ||
(si->si_strd = elf_getdata(scn, NULL)) == NULL)
elfterminate(file, "Cannot read strings for .symtab");
si->si_nument = si->si_shdr.sh_size / si->si_shdr.sh_entsize;
return (si);
}
void
symit_free(symit_data_t *si)
{
free(si);
}
void
symit_reset(symit_data_t *si)
{
si->si_next = 0;
}
char *
symit_curfile(symit_data_t *si)
{
return (si->si_curfile);
}
GElf_Sym *
symit_next(symit_data_t *si, int type)
{
GElf_Sym sym;
char *bname;
int check_sym = (type == STT_OBJECT || type == STT_FUNC);
for (; si->si_next < si->si_nument; si->si_next++) {
gelf_getsym(si->si_symd, si->si_next, &si->si_cursym);
gelf_getsym(si->si_symd, si->si_next, &sym);
si->si_curname = (caddr_t)si->si_strd->d_buf + sym.st_name;
if (GELF_ST_TYPE(sym.st_info) == STT_FILE) {
bname = strrchr(si->si_curname, '/');
si->si_curfile = bname == NULL ? si->si_curname : bname + 1;
}
if (GELF_ST_TYPE(sym.st_info) != type ||
sym.st_shndx == SHN_UNDEF)
continue;
if (check_sym && ignore_symbol(&sym, si->si_curname))
continue;
si->si_next++;
return (&si->si_cursym);
}
return (NULL);
}
char *
symit_name(symit_data_t *si)
{
return (si->si_curname);
}
|