/*
 *  ka2n.c by Davide Libenzi (Address to symbol conversion for linux kernel)
 *  Copyright (C) 2007  Davide Libenzi
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Davide Libenzi <davidel@xmailserver.org>
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KSYMS_FILE	"/proc/kallsyms"

static int find_symbol(const char *ksymfile, unsigned long long addr) {
	FILE *kfp;
	unsigned long long saddr, caddr;
	char ln[1024], csym[1024];

	if ((kfp = fopen(ksymfile, "r")) == NULL) {
		perror(ksymfile);
		return -1;
	}
	saddr = 0;
	while (fgets(ln, sizeof(ln), kfp) != NULL) {
		caddr = strtoull(ln, NULL, 16);
		if (caddr <= addr && caddr > saddr) {
			saddr = caddr;
			strcpy(csym, ln);
		}
	}
	fclose(kfp);
	if (saddr)
		fputs(csym, stdout);
	return 0;
}

int main(int ac, char **av) {
	int i;
	unsigned long long addr;
	const char *ksymfile = KSYMS_FILE;

	for (i = 1; i < ac; i++) {
		addr = strtoull(av[i], NULL, 16);
		find_symbol(ksymfile, addr);
	}

	return 0;
}

