[cfe-dev] .bss section in elf file

TheMask lol.themask at gmail.com
Sun Apr 6 14:36:00 PDT 2014


> Segments are used by the ELF loader (kernel or dynamic linker).
> Sectionsare normally used by tools operating on object files. A segment
> istypically a collection of multiple sections. Different granularity.

So, the kernel doesn't know about my section of /SHT_NOBITS/ type? the
/size/ tool print corretly the size of /.bss/ I've created but I'm not sure
if it's right. 
> .bss will be placed at the end of the data segment. Exact start addressis
> "size of data rounded up by bss alignment". You can compare addressand
> address + size of .data, .bss and the second (writeable) LOADsegment in
> the output of readelf -lS /bin/cat.

Before this answer I had created the section using this code (I didn't knew
but was looking for what does mean the second /LOAD/ which I see in a lot of
ELF executables). I dertermined the are of .bss section to write/read using
this:
int bss_area = start +sizeof(Elf32_Ehdr)       +sizeof(Elf32_phdr)      
+sizeof(code)        +sizeof(sec);
According to below code, is this right? it does work fine. Execute and
return expected return code. But I'm not sure which value put in some
sections. if it's wrong, what's the right way to create this sections? I
tried make code short as possible to your read but without loss context. But
if you want to make a full working version tell me I can post it.
#define START 0x08048000Elf32_shdr sec[5];	const char *names[] =	{		"",	
".shstrtab",		".text",		".rodata",                ".bss",	};	const char
msg[] = { 		0x48, 0x65, 0x6C, 0x6C, 0x6F, // msg: db 'Hello, World!', 10	
0x2C, 0x20, 0x57, 0x6F, 0x72,		0x6C, 0x64, 0x21, 0x0A	};	unsigned char
code[] = {		0xBA, 0x00, 0x00, 0x00, 0x00, // mov edx, length		0xB9, 0x00,
0x00, 0x00, 0x00, // mov ecx, msgptr		0xBB, 0x01, 0x00, 0x00, 0x00, // mov
ebx, 1		0xB8, 0x04, 0x00, 0x00, 0x00, // mov eax, 4		0xCD, 0x80                   
// int 0x80		0xBB, 0x00, 0x00, 0x00, 0x00, // mov ebx, 0		0xB8, 0x01, 0x00,
0x00, 0x00,  // mov eax, 1		0xCD, 0x80                     // int 0x80
};sec[0].sh_name  = 0;sec[0].sh_type  = SHT_null;// entry 1 -
.textsec[1].sh_name   = sec1_offset;sec[1].sh_type   =
SHT_progbits;sec[1].sh_flags  = SHF_alloc | SHF_execinstr;sec[1].sh_addr   =
START +	sizeof(Elf32_Ehdr)  +	sizeof(Elf32_phdr);sec[1].sh_offset =
sizeof(Elf32_Ehdr) +	sizeof(Elf32_phdr);sec[1].sh_size  = sizeof(code);//
entry 2 - .rodatasec[2].sh_name   = sec2_offset;sec[2].sh_type   =
SHT_progbits;sec[2].sh_flags  = SHF_alloc;sec[2].sh_align  =
0x4;sec[2].sh_addr   = 	START +	sizeof(Elf32_Ehdr)  +	sizeof(Elf32_phdr)  +
sizeof(code);sec[2].sh_offset = 	sizeof(Elf32_Ehdr) +	sizeof(Elf32_phdr) +
sizeof(code);sec[2].sh_size  =	sizeof(msg);// entry 3 -
.shstrtabsec[3].sh_name   = sec3_offset;sec[3].sh_type   =
SHT_strtab;sec[3].sh_flags  = SHF_alloc;sec[3].sh_offset = 
sizeof(Elf32_Ehdr) +	sizeof(Elf32_phdr) +	sizeof(msg)  +
sizeof(code);sec[3].sh_size   = section_length;// entry 4 -
.bsssec[4].sh_name   = sec2_name_offset; // pointer to '.bss' string in
namessec[4].sh_type   = SHT_nobits;sec[4].sh_flags  = SHF_alloc +
SHF_write;sec[4].sh_addr = // pointer to begging of this section at memory
START     +	sizeof(Elf32_Ehdr)       +	sizeof(Elf32_phdr)       +
sizeof(code)        +	sizeof(Elf32_phdr) * 3;sec[4].sh_offset =
sizeof(Elf32_Ehdr)       +	sizeof(Elf32_phdr)       +	sizeof(code)       
+sizeof(Elf32_Phdr) * 3;sec[4].sh_align = sizeof(int);sec[4].sh_size  =
sizeof(int) * 2;
And I fill the file with this:
	const char *outfile = "test";	FILE *fp = fopen(outfile, "w+b");	assert(fp);   
// pointer to msg     int msgptr =		START + sizeof(Elf32_Ehdr) + 
sizeof(Elf32_phdr) +		sizeof(code);	int msglen = sizeof(msg);	
memcpy(&code[1], &msglen, sizeof(int));	memcpy(&code[6], &msgptr,
sizeof(int));		fwrite(&ehdr,  sizeof(Elf32_Ehdr),  1, fp);	fwrite(&phdr, 
sizeof(Elf32_phdr),  1, fp);	fwrite(&code,  sizeof(code),        1, fp);
fwrite(msg,    sizeof(msg),         1, fp);	for(size_t i = 0; i <
sizeof(names) / sizeof(names[0]); i++)	{		fwrite(names[i], strlen(names[i])
+ 1, 1, fp);	}	fwrite(&sec,   sizeof(sec),        1, fp);	fclose(fp);

> Linkers and Loaders is still a decent starting point, I think. You canfind
> it either in a good library, Amazon or online
> viahttp://www.iecc.com/linker/ The wikipedia page for ELF has some
> furtherreferences, e.g. to the original SYSV specification for ELF.

Thanks very much. I will get this book.



--
View this message in context: http://clang-developers.42468.n3.nabble.com/bss-section-in-elf-file-tp4038737p4038771.html
Sent from the Clang Developers mailing list archive at Nabble.com.



More information about the cfe-dev mailing list