what's wrong with nlist

I'm trying to get the address of kernel symbols using nlist() function and this is not working on my machine (Solaris 10, 06/01, SPARC/64 bit):

I used the following program to test nlist:

[code]#include <stdio.h>

#include <stdlib.h>

#include <nlist.h>

#include <string.h>

int main()

{

struct nlist *knl = NULL;

int result;

knl = calloc(10, sizeof(struct nlist));

knl[0].n_name = strdup("spec_vnodeops");

result = nlist("/dev/ksyms", knl);

if ( result < 0 )

{

perror("Could not read namelist");

}

printf("Status %d\n", result);

}

[/code]

I compile and execute the code:

[code]

$ make

cc -c -o nlist.o -gnlist.c

cc -o knl -g-lelf nlist.o

$ ./knl

Could not read namelist: Error 0

Status -1

[/code]

But nlist() returns an error code. The problem is that it does not set errno or any other environment variable.

Is there a simple example on the net about how to use nlist to get kernel symbol address?

[1111 byte] By [pghoratiu] at [2007-11-26 8:31:44]
# 1

Actually in your case nlist() did not return error code. According to man page

nlist() returns 0 on success:

% man nlist

RETURN VALUES

All value entries are set to 0 if the file cannot be read or

if it does not contain a valid name list.

nlist() returns 0 on success, -1 on error.

So, it looks like you do not have permissions to read this file, or there is

no such name there.

Thanks,

Nik

NikMolchanov at 2007-7-6 21:54:31 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2
I think that i have the necessary access rights to run this. I tried running it as root and with sudo and got the same result (-1).I add also a stat() to the file before running nlist and that works fine.
pghoratiu at 2007-7-6 21:54:31 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3
Yes, you are right, I run your test as root and it returned -1. It returns the same result (-1) on Solaris 10, Solaris 9 and Solaris 8.But it seems to work on Solaris 7! I'll try to find if there is an open bug for this issue.Thanks,Nik
NikMolchanov at 2007-7-6 21:54:31 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 4

One more information that may help. I look-ed over the source code to understand it better, i think the problem is at this check in the _nlist() function implementation:

http://cvs.opensolaris.org/source/xref/on/usr/src/lib/libbc/libc/gen/common/_nl ist.c#55

[code]if ((fd == -1) || (lseek(fd, 0L, 0) == -1) ||

(read(fd, (char*)&buf, sizeof buf) != sizeof buf) || N_BADMAG(buf))

return (-1);

[/code]

pghoratiu at 2007-7-6 21:54:31 > top of Java-index,Development Tools,Solaris and Linux Development Tools...