[Info-vax] Non-portable code
John Reagan
johnrreagan at earthlink.net
Tue Jan 5 16:07:31 EST 2010
"Marc Van Dyck" <marc.vandyck at brutele.be> wrote in message
news:mn.2d1b7da1fb0d0bdb.104627 at brutele.be...
> G'd evening to all...
1) The location of the image ident is not at a fixed location on Itanium.
That information is buried inside the ".note" section. I have some C code
I'll put at the botton. You can convert to all Pascal if you wish.
Essentially you read the ELF header, find the offset to the section table,
scan down the section table looking for the NOTE section (we only generate
one), go to the NOTE section, scan down the note entries looking for the one
with the image ident string.
2) Pascal does have a conditional compilation facility (I added it back
around V5.7 or so). Look in the manual for the %IF, %THEN, %ELIF, %ENDIF
directives as well as the various %ARCH_NAME directive (and others). You
can also pass in values from the command line using the /CONSTANT qualifier
(akin to C's /DEFINE qualifier).
John
// File: elf_imginfo.c
// Author: Hartmut Becker
// Creation Date: 24-Sep-2004
//
// Follow the Elf structure and print some VMS image note entries.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <elfdef.h>
#include <descrip.h>
#define perrno(s) printf ("%s, errno: %d vaxc$errno: %%X%x\n", s, errno,
vaxc$errno)
extern sys$asctim() ;
int format_note_section (char *filename) ;
static void decode_note_entries (char *section, int size) ;
static const char magic_plus[] = {
EHDR$K_ELFMAG0, EHDR$K_ELFMAG1, EHDR$K_ELFMAG2, EHDR$K_ELFMAG3,
EHDR$K_ELFCLASS64, EHDR$K_ELFDATA2LSB
} ;
int main (int argc, char *argv[]) {
if (argc==1)
return printf ("-e-insarg, supply an ELF file.\n"), EXIT_FAILURE ;
if (format_note_section (argv[1]))
return EXIT_SUCCESS ;
return EXIT_FAILURE ;
}
int format_note_section (char *filename) {
FILE *fd ;
ELF64_EHDR the_elf_header ;
ELF64_SHDR *sh = NULL ;
char *section = NULL ;
int i ;
fd = fopen (filename, "rb") ;
if (!fd)
return perrno("-e-openerr"), 0 ;
if (fseek(fd, 0, SEEK_SET)!=0)
return perrno("-e-elfhdr"), 0 ;
if (fread(&the_elf_header, sizeof the_elf_header, 1, fd)!=1)
return perrno("-e-readehdr"), 0 ;
if (0!=strncmp((char*)&the_elf_header.ehdr$t_e_ident, magic_plus, sizeof
magic_plus-1)) {
fclose (fd) ;
printf ("-e-dunno, don't understand this file format\n") ;
return 0 ;
}
sh = (ELF64_SHDR*) malloc (sizeof *sh *the_elf_header.ehdr$w_e_shnum) ;
if (fseek(fd, the_elf_header.ehdr$q_e_shoff, SEEK_SET)!=0)
return perrno("-e-seekshdr"), 0 ;
if (fread(sh, sizeof *sh*the_elf_header.ehdr$w_e_shnum, 1, fd)!=1)
return perrno("-e-readshdr"), 0 ;
for (i=1; i<the_elf_header.ehdr$w_e_shnum; i++)
if (sh[i].shdr$l_sh_type==SHDR$K_SHT_NOTE) {
section = malloc (sh[i].shdr$q_sh_size) ;
if (fseek(fd, sh[i].shdr$q_sh_offset, SEEK_SET)!=0)
return perrno("-e-seeksec"), 0 ;
if (fread(section, sh[i].shdr$q_sh_size, 1, fd)!=1)
return perrno("-e-readsec"), 0 ;
decode_note_entries (section, sh[i].shdr$q_sh_size) ;
break ;
}
if (fclose(fd)!=0)
return perrno("-e-closerr"), 0 ;
return i<the_elf_header.ehdr$w_e_shnum ;
}
static void decode_note_entries (char *section, int size) {
ELF64_NHDR *nh ;
char *owner;
char *entry;
for (nh = (ELF64_NHDR*)section ; (char*)nh-section<size ;
nh=
(ELF64_NHDR*)((char*)nh+sizeof(ELF64_NHDR)+((nh->nhdr$q_nh_namesz+7)&~7)+((nh->nhdr$q_nh_descsz+7)&~7)))
{
if (nh->nhdr$q_nh_namesz>0)
owner = (char*)nh+sizeof(ELF64_NHDR) ;
else owner = "" ;
if (nh->nhdr$q_nh_descsz>0) {
if (nh->nhdr$q_nh_type == NHDR$K_NT_VMS_LINKTIME) {
char timbuf[32] ;
$DESCRIPTOR (timbuf_d, timbuf) ;
short int timlen ;
sys$asctim
(&timlen,&timbuf_d,(char*)nh+sizeof(ELF64_NHDR)+((nh->nhdr$q_nh_namesz+7)&~7)
,0) ;
printf ("owner: '%s', linktime: %.*s\n",owner, timlen,
timbuf) ;
}
else {
switch (nh->nhdr$q_nh_type) {
case NHDR$K_NT_VMS_IMGNAM:
entry = "imgnam" ;
break ;
case NHDR$K_NT_VMS_IMGID:
entry = "imgid" ;
break ;
case NHDR$K_NT_VMS_GSTNAM:
entry = "gstnam" ;
break ;
case NHDR$K_NT_VMS_IMGBID:
entry = "imgbid" ;
break ;
case NHDR$K_NT_VMS_LINKID:
entry = "linkid" ;
break ;
default:
entry = NULL ;
break ;
}
if (entry)
printf ("owner: '%s', %s: '%s'\n", owner, entry,
(char*)nh+sizeof(ELF64_NHDR)
+((nh->nhdr$q_nh_namesz+7)&~7) ) ;
}
}
}
}
More information about the Info-vax
mailing list