scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

elf64getsec.c (1034B)


      1 #include <stdio.h>
      2 
      3 #include <scc/mach.h>
      4 
      5 #include "../libmach.h"
      6 #include "elf64.h"
      7 
      8 Section *
      9 elf64getsec(Obj *obj, int *idx, Section *sec)
     10 {
     11 	int stype, n = *idx;
     12 	unsigned long flags, type;
     13 	unsigned sflags;
     14 	Elf64 *elf = obj->data;
     15 	Elf_Ehdr *hdr = &elf->hdr;
     16 	Elf_Shdr *shdr;
     17 
     18 	if (n >= elf->nsec)
     19 		return NULL;
     20 
     21 	shdr = &elf->shdr[n];
     22 	flags = shdr->sh_flags;
     23 	type = shdr->sh_type;
     24 
     25 	if (flags & SHF_ALLOC) {
     26 		if (type == SHT_NOBITS)
     27 			stype = 'B';
     28 		else if (flags & SHF_WRITE)
     29 			stype = 'D';
     30 		else
     31 			stype = 'T';
     32 	} else {
     33 		stype = 'N';
     34 	}
     35 
     36 	sflags = 0;
     37 	if (flags & SHF_WRITE)
     38 		sflags |= SWRITE;
     39 	if (flags  & SHF_EXECINSTR)
     40 		sflags |= SEXEC;
     41 	if (flags & SHF_ALLOC)
     42 		sflags |= SALLOC|SREAD;
     43 	if (type != SHT_NOBITS)
     44 		sflags |= SLOAD;
     45 	if (stype == 'T' || stype == 'D')
     46 		sflags |= SRELOC;
     47 
     48 	sec->name = elf64str(obj, SEC_STRTBL, shdr->sh_name);
     49 	sec->index = n;
     50 	sec->size = shdr->sh_size;
     51 	sec->base = shdr->sh_addr;
     52 	sec->type = stype;
     53 	sec->flags = sflags;
     54 	sec->align = shdr->sh_addralign;
     55 
     56 	return sec;
     57 }