17 ROSE_ASSERT(ef != NULL);
19 ROSE_ASSERT(p_name==NULL);
21 p_name->set_parent(
this);
55 if (local_data_pool!=NULL) {
56 free(local_data_pool);
57 local_data_pool = NULL;
77 if (get_file_alignment()>0) {
80 set_offset(new_offset);
81 changed = changed ?
true : (old_offset!=new_offset);
84 if (is_mapped() && get_mapped_alignment()>0) {
87 set_mapped_preferred_rva(new_rva);
88 changed = changed ?
true : (old_rva!=new_rva);
105 p_data = ef->
content(get_offset(), get_size());
120 if (local_data_pool!=NULL)
121 free(local_data_pool);
122 local_data_pool = (
unsigned char*)calloc(nbytes, 1);
141 p_name->set_parent(NULL);
147 set_isModified(
true);
157 if (p_short_name.empty())
183 set_isModified(
true);
200 set_isModified(
true);
208 return get_offset() + get_size();
215 return Extent(get_offset(), get_size());
222 return (get_mapped_preferred_rva()!=0 || get_mapped_size()!=0 ||
223 get_mapped_rperm() || get_mapped_wperm() || get_mapped_xperm());
231 set_mapped_preferred_rva(0);
232 set_mapped_actual_va(0);
233 set_mapped_rperm(
false);
234 set_mapped_wperm(
false);
235 set_mapped_xperm(
false);
242 ROSE_ASSERT(
this != NULL);
243 return p_mapped_size;
251 ROSE_ASSERT(
this != NULL);
252 if (p_mapped_size!=size)
253 set_isModified(
true);
254 p_mapped_size = size;
261 ROSE_ASSERT(
this != NULL);
262 return p_mapped_preferred_rva;
269 ROSE_ASSERT(
this != NULL);
270 if (p_mapped_preferred_rva!=a)
271 set_isModified(
true);
272 p_mapped_preferred_rva = a;
279 ROSE_ASSERT(
this != NULL);
281 return get_base_va() + get_mapped_preferred_rva();
289 ROSE_ASSERT(
this != NULL);
302 ROSE_ASSERT(
this != NULL);
303 return Extent(get_mapped_preferred_rva(), get_mapped_size());
315 ROSE_ASSERT(file!=NULL);
316 return file->
read_content(start_offset, dst_buf, size, strict);
330 ROSE_ASSERT(file!=NULL);
331 return file->
read_content(map, start_va, dst_buf, size, strict);
343 ROSE_ASSERT(file!=NULL);
344 if (start_offset > get_size()) {
346 throw ShortRead(
this, start_offset, size);
348 }
else if (start_offset+size > get_size()) {
350 throw ShortRead(
this, get_size(), start_offset+size-get_size());
351 retval = get_size() - start_offset;
356 file->
read_content(get_offset()+start_offset, dst_buf, retval,
true);
357 memset((
char*)dst_buf+retval, 0, size-retval);
367 unsigned char *buf =
new unsigned char[size];
368 read_content_local(rel_offset, buf, size,
false);
369 for (
size_t i=0; i<size; i++)
370 retval.push_back(buf[i]);
383 ROSE_ASSERT(file!=NULL);
394 ROSE_ASSERT(file!=NULL);
404 static char *buf=NULL;
405 static size_t nalloc=0;
409 if (nused >= nalloc) {
410 nalloc =
std::max((
size_t)32, 2*nalloc);
411 buf = (
char*)realloc(buf, nalloc);
412 ROSE_ASSERT(buf!=NULL);
416 read_content_local(rel_offset+nused, &byte, 1, strict);
418 return std::string(buf, nused);
433 read_content_local(*rel_offset, &byte, 1, strict);
435 ROSE_ASSERT(shift<64);
436 retval |= (byte & 0x7f) << shift;
438 if (0==(byte & 0x80))
454 read_content_local(*rel_offset, &byte, 1, strict);
456 ROSE_ASSERT(shift<64);
457 retval |= (byte & 0x7f) << shift;
459 if (0==(byte & 0x80))
462 retval = (retval << (64-shift)) >> (64-shift);
482 size_t nwrite, nzero;
484 ROSE_ASSERT(
this != NULL);
487 if (offset>=get_size()) {
490 }
else if (offset+bufsize<=get_size()) {
494 nwrite = get_size() -
offset;
495 nzero = bufsize - nwrite;
499 f.seekp(0, std::ios::end);
501 while (nwrite>0 && 0==((
const char*)buf)[nwrite-1] && get_offset()+offset+nwrite>filesize)
508 off_t o = get_offset() +
offset;
511 f.write((
const char*)buf, nwrite);
515 for (
size_t i=nwrite; i<bufsize; i++) {
516 if (((
const char*)buf)[i]) {
518 sprintf(mesg,
"non-zero value truncated: buf[0x%zx]=0x%02x", i, ((
const unsigned char*)buf)[i]);
519 fprintf(stderr,
"SgAsmGenericSection::write: error: %s", mesg);
520 fprintf(stderr,
" in [%d] \"%s\"\n", get_id(),
get_name()->get_string(
true).c_str());
521 fprintf(stderr,
" section is at file offset 0x%08"PRIx64
" (%"PRIu64
"), size 0x%"PRIx64
" (%"PRIu64
") bytes\n",
522 get_offset(), get_offset(), get_size(), get_size());
523 fprintf(stderr,
" write %zu byte%s at section offset 0x%08"PRIx64
"\n", bufsize, 1==bufsize?
"":
"s", offset);
524 fprintf(stderr,
" ");
527 hexdump(stderr, get_offset()+offset, (
const unsigned char*)buf, bufsize, hf);
528 fprintf(stderr,
"\n");
533 return offset+bufsize;
542 return write(f, offset, buf.
size(), &(buf[0]));
551 return write(f, offset, buf.size(), (
void*)&(buf[0]));
558 return write(f, offset, str.size(), &(str[0]));
565 return write(f, offset, 1, &c);
576 unsigned char byte = val & 0x7f;
580 buf[offset++] = byte;
592 }
else if (val==-1) {
593 buf[offset++] = 0x7f;
595 while (val!=0 && val!=-1) {
596 unsigned char byte = (uint64_t)val & 0x7f;
598 if (val!=0 && val!=-1)
600 buf[offset++] = byte;
615 Extent s(get_offset(), get_size());
616 const ExtentMap &file_extents = get_file()->get_referenced_extents();
630 assert(!
"invalid extent overlap category");
651 ROSE_ASSERT(get_file() != NULL);
652 ROSE_ASSERT(get_file()->get_tracking_references());
656 rose_addr_t new_end = std::min(get_file()->get_orig_size(), get_offset()+new_size);
657 if (get_offset()<=new_end) {
658 p_data.resize(new_end-get_offset());
660 ROSE_ASSERT(0==p_data.size());
663 if (p_size!=new_size)
664 set_isModified(
true);
688 fprintf(stderr,
"SgAsmGenericSection::unparse(FILE*) for section [%d] \"%s\"\n",
id,
name.c_str());
700 assert(e.
first()+e.
size() <= get_size());
701 const unsigned char *extent_data;
703 if (e.
first() >= p_data.size()) {
706 }
else if (e.
first() + e.
size() > p_data.size()) {
707 extent_data = &p_data[e.
first()];
708 nwrite = p_data.size() - e.
first();
710 extent_data = &p_data[e.
first()];
714 write(f, e.
first(), e.
size(), extent_data);
723 ExtentMap holes = get_unreferenced_extents();
724 fprintf(stderr,
"Section \"%s\", 0x%"PRIx64
" bytes\n",
get_name()->get_string(
true).c_str(), get_size());
736 return get_va_offset(rva + get_base_va());
745 ROSE_ASSERT(is_mapped());
746 ROSE_ASSERT(va >= get_base_va());
748 ROSE_ASSERT(rva >= get_mapped_preferred_rva());
749 return get_offset() + (rva - get_mapped_preferred_rva());
757 for (
size_t i=0; i<slist.size(); i++) {
761 fprintf(f,
"%-*s is 0x%08"PRIx64
" (%"PRIu64
") bytes into section [%d] \"%s\"\n",
773 sprintf(p,
"%sSection[%zd].", prefix, idx);
775 sprintf(p,
"%sSection.", prefix);
780 fprintf(f,
"%s%-*s = \"%s\"", p, w,
"name", p_name->get_string(
true).c_str());
781 if (!p_short_name.empty())
782 fprintf(f,
" (%s)", p_short_name.c_str());
784 fprintf(f,
"%s%-*s = %d\n", p, w,
"id", p_id);
785 fprintf(f,
"%s%-*s = 0x%08"PRIx64
" (%"PRIu64
") bytes into file\n", p, w,
"offset",
p_offset,
p_offset);
786 fprintf(f,
"%s%-*s = 0x%08"PRIx64
" (%"PRIu64
") bytes\n", p, w,
"size", get_size(), get_size());
787 if (0==get_file_alignment()) {
788 fprintf(f,
"%s%-*s = not specified\n", p, w,
"file_alignment");
790 fprintf(f,
"%s%-*s = 0x%08"PRIx64
" (%"PRIu64
") %s\n", p, w,
"file_alignment",
791 get_file_alignment(), get_file_alignment(),
792 0==get_offset()%get_file_alignment()?
"satisfied":
"NOT SATISFIED");
794 fprintf(f,
"%s%-*s = %s\n", p, w,
"synthesized", p_synthesized?
"yes":
"no");
796 fprintf(f,
"%s%-*s = \"%s\"\n", p, w,
"header", p_header->get_name()->get_string(
true).c_str());
798 fprintf(f,
"%s%-*s = not associated\n", p, w,
"header");
802 fprintf(f,
"%s%-*s = %s\n", p, w,
"purpose", purpose.c_str());
805 fprintf(f,
"%s%-*s = rva=0x%08"PRIx64
", size=%"PRIu64
" bytes\n", p, w,
"mapped", p_mapped_preferred_rva, p_mapped_size);
806 if (0==get_mapped_alignment()) {
807 fprintf(f,
"%s%-*s = not specified\n", p, w,
"mapped_alignment");
809 fprintf(f,
"%s%-*s = 0x%08"PRIx64
" (%"PRIu64
") %s\n", p, w,
"mapped_alignment",
810 get_mapped_alignment(), get_mapped_alignment(),
811 0==get_mapped_preferred_rva()%get_mapped_alignment()?
"satisfied":
"NOT SATISFIED");
813 fprintf(f,
"%s%-*s = %c%c%c\n", p, w,
"permissions",
814 get_mapped_rperm()?
'r':
'-', get_mapped_wperm()?
'w':
'-', get_mapped_xperm()?
'x':
'-');
816 fprintf(f,
"%s%-*s = <not mapped>\n", p, w,
"mapped");
819 fprintf(f,
"%s%-*s = %s\n", p, w,
"contains_code", get_contains_code()?
"true":
"false");
820 fprintf(f,
"%s%-*s = 0x%08"PRIx64
" (%"PRIu64
") \n", p, w,
"mapped_actual_va", p_mapped_actual_va, p_mapped_actual_va);
825 hexdump(f, 0, std::string(p)+
"data at ", p_data);