Exemplo n.º 1
0
int
handle_file(const char *name)
{
	int fd, rt;

	if (name == NULL)
		return (RETURN_NOINPUT);
	if (strcmp("{standard input}", name) != 0) {
		if (freopen(name, "rb", stdin) == NULL) {
			warnx("'%s': %s", name, strerror(errno));
			return (RETURN_NOINPUT);
		}
	} else {
		return (find_strings(name, (off_t)0, (off_t)0));
	}

	fd = fileno(stdin);
	if (fd < 0)
		return (RETURN_NOINPUT);
	rt = handle_elf(name, fd);
	return (rt);
}
Exemplo n.º 2
0
/*
 * size utility using elf(3) and gelf(3) API to list section sizes and
 * total in elf files. Supports only elf files (core dumps in elf
 * included) that can be opened by libelf, other formats are not supported.
 */
int
main(int argc, char **argv)
{
	int ch, r, rc;
	const char **files, *fn;

	rc = RETURN_OK;

	if (elf_version(EV_CURRENT) == EV_NONE)
		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
		    elf_errmsg(-1));

	while ((ch = getopt_long(argc, argv, "ABVdhotx", size_longopts,
	    NULL)) != -1)
		switch((char)ch) {
		case 'A':
			style = STYLE_SYSV;
			break;
		case 'B':
			style = STYLE_BERKELEY;
			break;
		case 'V':
			show_version();
			break;
		case 'd':
			radix = RADIX_DECIMAL;
			break;
		case 'o':
			radix = RADIX_OCTAL;
			break;
		case 't':
			show_totals = 1;
			break;
		case 'x':
			radix = RADIX_HEX;
			break;
		case 0:
			switch (size_option) {
			case OPT_FORMAT:
				if (*optarg == 's' || *optarg == 'S')
					style = STYLE_SYSV;
				else if (*optarg == 'b' || *optarg == 'B')
					style = STYLE_BERKELEY;
				else {
					warnx("unrecognized format \"%s\".",
					      optarg);
					usage();
				}
				break;
			case OPT_RADIX:
				r = strtol(optarg, NULL, 10);
				if (r == 8)
					radix = RADIX_OCTAL;
				else if (r == 10)
					radix = RADIX_DECIMAL;
				else if (r == 16)
					radix = RADIX_HEX;
				else {
					warnx("unsupported radix \"%s\".",
					      optarg);
					usage();
				}
				break;
			default:
				err(EXIT_FAILURE, "Error in option handling.");
				/*NOTREACHED*/
			}
			break;
		case 'h':
		case '?':
		default:
			usage();
			/* NOTREACHED */
		}
	argc -= optind;
	argv += optind;

	files = (argc == 0) ? default_args : (void *) argv;

	while ((fn = *files) != NULL) {
		rc = handle_elf(fn);
		if (rc != RETURN_OK)
			warnx(rc == RETURN_NOINPUT ?
			      "'%s': No such file" :
			      "%s: File format not recognized", fn);
		files++;
	}
	if (style == STYLE_BERKELEY) {
		if (show_totals)
			berkeley_totals();
		tbl_flush();
	}
        return (rc);
}
Exemplo n.º 3
0
/*
 * That;s the tool to generate patches object files.
 */
int main(int argc, char *argv[])
{
	struct stat st;
	int opt, idx;
	void *mem;
	int fd;

	static const char short_opts[] = "f:o:s:p:v:r:h";
	static struct option long_opts[] = {
		{ "file",	required_argument,	0, 'f' },
		{ "output",	required_argument,	0, 'o' },
		{ "stream",	required_argument,	0, 's' },
		{ "sym-prefix",	required_argument,	0, 'p' },
		{ "variable",	required_argument,	0, 'v' },
		{ "pcrelocs",	required_argument,	0, 'r' },
		{ "help",	required_argument,	0, 'h' },
		{ },
	};

	if (argc < 3)
		goto usage;

	while (1) {
		idx = -1;
		opt = getopt_long(argc, argv, short_opts, long_opts, &idx);
		if (opt == -1)
			break;
		switch (opt) {
		case 'f':
			opts.input_filename = optarg;
			break;
		case 'o':
			opts.output_filename = optarg;
			break;
		case 's':
			opts.stream_name = optarg;
			break;
		case 'p':
			opts.prefix_name = optarg;
			break;
		case 'v':
			opts.var_name = optarg;
			break;
		case 'r':
			opts.nrgotpcrel_name = optarg;
			break;
		case 'h':
		default:
			goto usage;
		}
	}

	if (!opts.input_filename)
		goto usage;

	fd = open(opts.input_filename, O_RDONLY);
	if (fd < 0) {
		pr_perror("Can't open file %s", opts.input_filename);
		goto err;
	}

	if (fstat(fd, &st)) {
		pr_perror("Can't stat file %s", opts.input_filename);
		goto err;
	}

	fout = fopen(opts.output_filename, "w");
	if (fout == NULL) {
		pr_perror("Can't open %s", opts.output_filename);
		goto err;
	}

	mem = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
	if (mem == MAP_FAILED) {
		pr_perror("Can't mmap file %s", opts.input_filename);
		goto err;
	}

	if (handle_elf(mem, st.st_size)) {
		fclose(fout);
		unlink(opts.output_filename);
		goto err;
	}
	fclose(fout);
	printf("%s generated successfully.\n", opts.output_filename);
	return 0;
usage:
	fprintf(stderr, "Usage: %s -f filename\n", argv[0]);
err:
	return 1;
}