Ejemplo n.º 1
0
/** Reads a string from the file. The string begins at the specified absolute file offset and continues until the first NUL
 *  byte or end of file is reached. However, if @p strict is set (the default) and we reach the end-of-file then an
 *  SgAsmExecutableFileFormat::ShortRead exception is thrown. */
std::string
SgAsmGenericSection::read_content_str(rose_addr_t abs_offset, bool strict)
{
    SgAsmGenericFile *file = get_file();
    ROSE_ASSERT(file!=NULL);
    return file->read_content_str(abs_offset, strict);
}
Ejemplo n.º 2
0
/** Reads data from a file. Reads up to @p size bytes of data beginning at byte @p start_va (absolute virtual address) in the
 *  mapped address space and placing the results in @p dst_buf and returning the number of bytes read. The return value could
 *  be smaller than @p size if the reading encounters virtual addresses that are not mapped.  When an unmapped virtual address
 *  is encountered the reading stops (even if subsequent virtual addresses are defined) and one of two things happen: if @p
 *  strict is set (the default) then an MemoryMap::NotMapped exception is thrown, otherwise the @p dst_buf is padded with zeros
 *  so that all @p size bytes are initialized. The @p map is used to map virtual addresses to file offsets; if @p map is NULL
 *  then the map defined in the underlying file is used. */
size_t
SgAsmGenericSection::read_content(const MemoryMap *map, rose_addr_t start_va, void *dst_buf, rose_addr_t size, bool strict)
{
    SgAsmGenericFile *file = get_file();
    ROSE_ASSERT(file!=NULL);
    return file->read_content(map, start_va, dst_buf, size, strict);
}
Ejemplo n.º 3
0
/** Reads a string from the file. The string begins at the specified virtual address and continues until the first NUL byte or
 *  until we reach an address that is not mapped. However, if @p strict is set (the default) and we reach an unmapped address
 *  then an MemoryMap::NotMapped exception is thrown. The @p map defines the mapping from virtual addresses to file offsets;
 *  if @p map is NULL then the map defined in the underlying file is used. */
std::string
SgAsmGenericSection::read_content_str(const MemoryMap *map, rose_addr_t start_va, bool strict)
{
    SgAsmGenericFile *file = get_file();
    ROSE_ASSERT(file!=NULL);
    return file->read_content_str(map, start_va, strict);
}
Ejemplo n.º 4
0
/** Reads data from a file. Reads up to @p size bytes of data beginning at byte @p start_offset from the beginning of the file,
 *  placing the results in @p dst_buf and returning the number of bytes read. The return value could be smaller than @p size
 *  if the end-of-file is reached. If the return value is smaller than @p size then one of two things happen: if @p strict is
 *  set (the default) then an SgAsmExecutableFileFormat::ShortRead exception is thrown; otherwise the @p dst_buf will be
 *  padded with zero bytes so that exactly @p size bytes of @p dst_buf are always initialized. */
size_t
SgAsmGenericSection::read_content(rose_addr_t start_offset, void *dst_buf, rose_addr_t size, bool strict)
{
    SgAsmGenericFile *file = get_file();
    ROSE_ASSERT(file!=NULL);
    return file->read_content(start_offset, dst_buf, size, strict);
}
Ejemplo n.º 5
0
/* Destructor must remove section/header link */
SgAsmGenericSection::~SgAsmGenericSection()
{
    SgAsmGenericFile* ef = get_file();
    SgAsmGenericHeader *hdr = get_header();

    /* See constructor comment. This deletes both halves of the header/section link. */
    if (hdr) {
        hdr->remove_section(this);
        set_header(NULL);
    }

    /* See comment in ROSETTA/src/binaryInstruction.C.  We need to explicitly delete the section name. */
    if (p_name) {
        SageInterface::deleteAST(p_name);
        p_name = NULL;
    }
    
    /* FIXME: holes should probably be their own class, which would make the file/hole bidirectional linking more like the
     *        header/section bidirectional links (RPM 2008-09-02) */
    ef->remove_hole(this);

    /* Delete children */
    p_file   = NULL; // deleted by SageInterface::deleteAST()

    /* If the section has allocated its own local pool for the p_data member (rather than pointing into the SgAsmGenericFile)
     * then free that now. */
    if (local_data_pool!=NULL) {
        free(local_data_pool);
        local_data_pool = NULL;
    }
}
Ejemplo n.º 6
0
bool
BinaryLoader::is_linked(SgAsmInterpretation *interp, const std::string &filename)
{
    const SgAsmGenericHeaderPtrList &headers = interp->get_headers()->get_headers();
    for (SgAsmGenericHeaderPtrList::const_iterator hi=headers.begin(); hi!=headers.end(); ++hi) {
        SgAsmGenericFile *file = (*hi)->get_file();
        ASSERT_not_null(file);
        if (file->get_name()==filename)
            return true;
    }
    return false;
}
Ejemplo n.º 7
0
void
InitPointerToNull::run(SgNode* fileA, SgNode* fileB) {
  instance=NULL;
  if (!testFlag)
    instance = QROSE::cbData<BinQGUI *>();

  if (isSgProject(fileA)==NULL && isSgAsmBlock(fileA)==NULL) {
    cerr << "This is not a valid file for this analysis!" << endl;
    if (!testFlag) {
      QString res = QString("This is not a valid file for this analysis");
      instance->analysisResult->append(res);  
    }
    return;
  }

  project=false;
  SgAsmGenericFile* file = NULL;
  if (isSgProject(fileA)) {
    project=true;
    SgBinaryComposite* binary = isSgBinaryComposite(isSgProject(fileA)->get_fileList()[0]);
    file = binary != NULL ? binary->get_binaryFile() : NULL;
    ROSE_ASSERT(file);
    info = new VirtualBinCFG::AuxiliaryInformation(file);
  }
  else
    info = new VirtualBinCFG::AuxiliaryInformation(fileA);

  if (!testFlag) {
    ROSE_ASSERT(instance);
    ROSE_ASSERT(instance->analysisTab);
    instance->analysisTab->setCurrentIndex(1);
    if (isSgProject(fileA)) {
      QString res = QString("Looking at dynamic information : %1").arg(file->get_name().c_str());
      instance->analysisResult->append(res);  
    }
  }

  if (isSgProject(fileA)) {
    genericF = file;
    runTraversal(isSgProject(fileA));
  }
  else
    runTraversal(isSgAsmBlock(fileA));


  if (instance) {
    QString res = QString("\n>>>>>>>>>>>>>>>> Resolving call addresses to names ...");
    instance->analysisResult->append(res);  
  }
}
Ejemplo n.º 8
0
/* class method */
SgAsmGenericFile* 
BinaryLoader::createAsmAST(SgBinaryComposite* binaryFile, std::string filePath)
{
    ASSERT_forbid(filePath.empty());
  
    SgAsmGenericFile* file = SgAsmExecutableFileFormat::parseBinaryFormat(filePath.c_str());
    ASSERT_not_null(file);
  
    // TODO do I need to attach here - or can I do after return
    binaryFile->get_genericFileList()->get_files().push_back(file);
    file->set_parent(binaryFile->get_genericFileList());

    /* Add a new interpretation to the SgBinaryComposite object for each header of the newly parsed
     * SgAsmGenericFile for which a suitable interpretation does not already exist. */
    const SgAsmGenericHeaderPtrList &headers = file->get_headers()->get_headers();
    SgAsmInterpretationPtrList &interps = binaryFile->get_interpretations()->get_interpretations();
    for (size_t i = 0; i < headers.size(); ++i) {
        SgAsmGenericHeader* header = headers[i];
        SgAsmInterpretation* interp = NULL;
        for (size_t j = 0; j < interps.size(); ++j) {
            ASSERT_forbid(interps[j]->get_headers()->get_headers().empty());
            SgAsmGenericHeader* interpHeader = interps[j]->get_headers()->get_headers().front();
            if (isHeaderSimilar(header, interpHeader)) {
                interp = interps[j];
                break;
            }
        }
        if (!interp) {
            interp = new SgAsmInterpretation();
            interps.push_back(interp);
            interp->set_parent(binaryFile->get_interpretations());
            if (const RegisterDictionary *registers = RegisterDictionary::dictionary_for_isa(header->get_isa()))
                interp->set_registers(registers);
        }
        interp->get_headers()->get_headers().push_back(header);
    }

#if USE_ROSE_DWARF_SUPPORT
    /* Parse Dwarf info and add it to the SgAsmGenericFile. */
    readDwarf(file); 
#endif
  
  return file;
}
Ejemplo n.º 9
0
/** Reads data from a file. This behaves the same as read_content() except the @p start_offset is relative to the beginning of
 *  this section.   Reading past the end of the section is not allowed and treated as a short read, and one of two things
 *  happen: if @p strict is set (the default) then an SgAsmExecutableFileFormat::ShortRead exception is thrown, otherwise the
 *  result is zero padded so as to contain exactly @p size bytes. */
size_t
SgAsmGenericSection::read_content_local(rose_addr_t start_offset, void *dst_buf, rose_addr_t size, bool strict)
{
    size_t retval;
    SgAsmGenericFile *file = get_file();
    ROSE_ASSERT(file!=NULL);
    if (start_offset > get_size()) {
        if (strict)
            throw ShortRead(this, start_offset, size);
        retval = 0;
    } else if (start_offset+size > get_size()) {
        if (strict)
            throw ShortRead(this, get_size(), start_offset+size-get_size());
        retval = get_size() - start_offset;
    } else {
        retval = size;
    }

    file->read_content(get_offset()+start_offset, dst_buf, retval, true);
    memset((char*)dst_buf+retval, 0, size-retval);
    return retval;
}
Ejemplo n.º 10
0
/** Saves a reference to the original file data for a section based on the sections current offset and size. Once we do this,
 *  changing the offset or size of the file will not affect the original data. The original data can be extended, however, by
 *  calling SgAsmGenericSection::extend(), which is typically done during parsing. */
void
SgAsmGenericSection::grab_content()
{
    SgAsmGenericFile *ef = get_file();
    ROSE_ASSERT(ef);

    if (get_offset()<=ef->get_orig_size()) {
        if (get_offset()+get_size()<=ef->get_orig_size()) {
            p_data = ef->content(get_offset(), get_size());
        } else {
            p_data = ef->content(get_offset(), ef->get_orig_size()-get_offset());
        }
    }
}
Ejemplo n.º 11
0
/* Maps the sections of a single header. */
void
BinaryLoader::remap(MemoryMap *map, SgAsmGenericHeader *header)
{
    SgAsmGenericFile *file = header->get_file();
    ASSERT_not_null(file);

    Stream trace(mlog[TRACE]);

    trace <<"remapping sections of " <<header->get_file()->get_name() <<"\n";
    SgAsmGenericSectionPtrList sections = get_remap_sections(header);

    rose_addr_t old_base_va = header->get_base_va();
    rose_addr_t new_base_va = rebase(map, header, sections);
    if (new_base_va != old_base_va) {
        trace <<"  temporarily rebasing header from " <<StringUtility::addrToString(old_base_va)
              <<" to " <<StringUtility::addrToString(new_base_va) <<"\n";
        header->set_base_va(new_base_va);
    }

    try {
        for (SgAsmGenericSectionPtrList::iterator si=sections.begin(); si!=sections.end(); ++si) {
            SgAsmGenericSection *section = *si;
            section->set_mapped_actual_va(0); /*reset in case previously mapped*/

            if (trace) {
                trace <<"  mapping section [" <<section->get_id() <<"] \"" <<section->get_name()->get_string(true) <<"\"";
                if (section->get_base_va()!=0)
                    trace <<" with base va " <<StringUtility::addrToString(section->get_base_va());
                trace <<"\n";
                trace <<"    Specified RVA:       " <<StringUtility::addrToString(section->get_mapped_preferred_rva())
                      <<" + " <<StringUtility::addrToString(section->get_mapped_size()) <<" bytes"
                      <<" = " <<StringUtility::addrToString(section->get_mapped_preferred_rva()+section->get_mapped_size())
                      <<"\n";
                if (section->get_base_va()!=0) {
                    trace <<"    Specified  VA:       "
                          <<StringUtility::addrToString(section->get_base_va() + section->get_mapped_preferred_rva()) <<" + "
                          <<StringUtility::addrToString(section->get_mapped_size()) <<" bytes = "
                          <<StringUtility::addrToString(section->get_base_va() + section->get_mapped_preferred_rva() +
                                                        section->get_mapped_size()) <<"\n";
                }
                trace <<"    Specified offset:    "
                      <<StringUtility::addrToString(section->get_offset()) <<" + "
                      <<StringUtility::addrToString(section->get_size()) <<" bytes = "
                      <<StringUtility::addrToString(section->get_offset()+section->get_size()) <<"\n";
                trace <<"    Specified alignment: memory=[" <<section->get_mapped_alignment() <<","
                      <<section->get_mapped_alignment() <<"], file=["
                      <<section->get_file_alignment() <<"," <<section->get_file_alignment() <<"]\n";
            }

            /* Figure out alignment, etc. */
            rose_addr_t malign_lo=1, malign_hi=1, va=0, mem_size=0, offset=0, file_size=0, va_offset=0;
            bool anon_lo=true, anon_hi=true, map_private=false;
            ConflictResolution resolve = RESOLVE_THROW;
            MappingContribution contrib = align_values(section, map,                      /* inputs */
                                                       &malign_lo, &malign_hi,            /* alignment outputs */
                                                       &va, &mem_size,                    /* memory location outputs */
                                                       &offset, &file_size, &map_private, /* file location outputs */
                                                       &va_offset, &anon_lo, &anon_hi,    /* internal location outputs */
                                                       &resolve);                         /* conflict resolution output */
            rose_addr_t falign_lo = std::max(section->get_file_alignment(), (rose_addr_t)1);
            rose_addr_t falign_hi = falign_lo;

            if (trace) {
                if (CONTRIBUTE_NONE==contrib || 0==mem_size) {
                    trace <<"    Does not contribute to map\n";
                } else {
                    trace <<"    Adjusted alignment:  memory=["
                          <<malign_lo <<"," <<malign_hi <<"], file=[" <<falign_lo <<"," <<falign_hi <<"]\n";
                    trace <<"    Aligned VA:          "
                          <<StringUtility::addrToString(va) <<" + "
                          <<StringUtility::addrToString(mem_size) <<" bytes = "
                          <<StringUtility::addrToString(va+mem_size);
                    if (section->get_base_va()+section->get_mapped_preferred_rva()==va &&
                        section->get_mapped_size()==mem_size) {
                        trace <<" (no change)\n";
                    } else {
                        trace <<"\n";
                    }
                    if (va < new_base_va) {
                        trace <<"    WARNING: aligned va " <<StringUtility::addrToString(va) <<" is less than "
                              <<(new_base_va==old_base_va?"":"temporary ") <<"base va "
                              <<StringUtility::addrToString(new_base_va) <<"\n";
                    }
                    if (CONTRIBUTE_ADD==contrib) {
                        trace <<"    Aligned offset:      " <<StringUtility::addrToString(offset) <<" + "
                              <<StringUtility::addrToString(file_size) <<" bytes = "
                              <<StringUtility::addrToString(offset+file_size)
                              <<(section->get_offset()==offset && section->get_size()==file_size ? " (no change)\n" : "\n");
                        trace <<"    Permissions:         "
                              <<(section->get_mapped_rperm()?'r':'-')
                              <<(section->get_mapped_wperm()?'w':'-')
                              <<(section->get_mapped_xperm()?'x':'-') <<"\n";
                        trace <<"    Internal offset:     " <<StringUtility::addrToString(va_offset)
                              <<" (va " <<StringUtility::addrToString(va+va_offset) <<")\n";
                    }
                }
            }

            /* Sanity checks */
            if (CONTRIBUTE_NONE==contrib || 0==mem_size)
                continue;
            ASSERT_require(va_offset<mem_size);
            if (file_size>mem_size) file_size = mem_size;
            ASSERT_require(va + va_offset >= header->get_base_va());
            if (trace) {
                trace <<"    Current memory map (before we map this section)\n";
                map->dump(trace, "        ");
            }

            /* Erase part of the mapping? */
            if (CONTRIBUTE_SUB==contrib) {
                trace <<"    Subtracting contribution\n";
                map->erase(AddressInterval::baseSize(va, mem_size));
                continue;
            }

            /* Resolve mapping conflicts.  The new mapping may have multiple parts, so we test whether all those parts can be
             * mapped by first mapping a region and then removing it.  In this way we can perform the test atomically rather
             * than trying to undo the parts that had been successful. Allocating a large region does not actually allocate any
             * memory. */
            try {
                map->insert(AddressInterval::baseSize(va, mem_size), MemoryMap::Segment::nullInstance(mem_size));
                map->erase(AddressInterval::baseSize(va, mem_size));
            } catch (const MemoryMap::Exception&) {
                switch (resolve) {
                    case RESOLVE_THROW:
                        throw;
                    case RESOLVE_OVERMAP:
                        trace <<"    Conflict: resolved by making a hole\n";
                        map->erase(AddressInterval::baseSize(va, mem_size));
                        break;
                    case RESOLVE_REMAP:
                    case RESOLVE_REMAP_ABOVE: {
                        trace <<"    Unable to map entire desired region.\n";
                        AddressInterval where = AddressInterval::hull(RESOLVE_REMAP_ABOVE==resolve ? va : 0,
                                                                      AddressInterval::whole().greatest());
                        rose_addr_t new_va = 0;
                        if (!map->findFreeSpace(mem_size, malign_lo, where).assignTo(new_va)) {
                            throw MemoryMap::NoFreeSpace("unable to allocate space in specimen memory map",
                                                         map, mem_size);
                        }
                        ASSERT_require2(0 == (new_va+mem_size) % malign_hi, "FIXME: not handled yet [RPM 2010-09-03]");
                        va = new_va;
                        trace <<"    Relocated to VA:     " <<StringUtility::addrToString(va) <<" + "
                              <<StringUtility::addrToString(mem_size) <<" bytes = "
                              <<StringUtility::addrToString(va + mem_size) <<"\n";
                        break;
                    }
                }
            }

            /* Save the virtual address where this section is (will be) mapped.  When a section is mapped more than once
             * (perfectly legal to do so) only the last mapping is saved. */
            section->set_mapped_actual_va(va + va_offset);

            /* Permissions */
            unsigned mapperms=0;
            if (section->get_mapped_rperm())
                mapperms |= MemoryMap::READABLE;
            if (section->get_mapped_wperm())
                mapperms |= MemoryMap::WRITABLE;
            if (section->get_mapped_xperm())
                mapperms |= MemoryMap::EXECUTABLE;

            /* Segment name for debugging. This is the file base name and section name concatenated. */
            std::string::size_type file_basename_pos = file->get_name().find_last_of("/");
            file_basename_pos = file_basename_pos==file->get_name().npos ? 0 : file_basename_pos+1;
            std::string melmt_name = file->get_name().substr(file_basename_pos) + "(" + section->get_name()->get_string() + ")";
            trace <<"    Map element name: " <<escapeString(melmt_name) <<"\n";

            /* Anonymously map the part of memory beyond the physical end of the file */
            SgAsmGenericFile *file = section->get_file();
            rose_addr_t total = file->get_data().size(); /*total size of file*/
            if (offset+mem_size > total) {
                rose_addr_t n, a;
                if (offset >= total) {
                    /* starts beyond EOF */
                    n = mem_size;
                    a = va;
                } else {
                    /* overlaps EOF */
                    n = (offset + mem_size) - total;
                    a = va + total - offset;
                }
                trace <<"    Mapping part beyond EOF(" <<StringUtility::addrToString(total) <<"):      "
                      <<"va=" <<StringUtility::addrToString(a) <<" + " <<StringUtility::addrToString(n) <<" = "
                      <<StringUtility::addrToString(a+n) <<"\n";
                map->insert(AddressInterval::baseSize(a, n),
                            MemoryMap::Segment::anonymousInstance(n, mapperms|MemoryMap::PRIVATE, melmt_name));
                mem_size -= n;
                file_size = std::min(file_size, mem_size);
            }

            /* Anonymously map the part of memory beyond the part of file */
            if (anon_hi && mem_size>file_size) {
                rose_addr_t n = mem_size - file_size;
                rose_addr_t a = va + file_size;
                trace <<"    Mapping part beyond end of section:        va="
                      <<StringUtility::addrToString(a) <<" + " <<StringUtility::addrToString(n) <<" = "
                      <<StringUtility::addrToString(a+n) <<"\n";
                map->insert(AddressInterval::baseSize(a, n),
                            MemoryMap::Segment::anonymousInstance(n, mapperms|MemoryMap::PRIVATE, melmt_name));
                mem_size -= n;
            }

            /* Anonymously map the part of memory before the section */
            if (anon_lo && va_offset>0 && mem_size>0) {
                rose_addr_t n = va_offset - va;
                rose_addr_t a = va;
                trace <<"    Mapping part before beginning of section: va="
                      <<StringUtility::addrToString(a) <<" + " <<StringUtility::addrToString(n) <<" = "
                      <<StringUtility::addrToString(a+n) <<"\n";
                map->insert(AddressInterval::baseSize(a, n),
                            MemoryMap::Segment::anonymousInstance(n, mapperms|MemoryMap::PRIVATE, melmt_name));
                mem_size -= n;
                file_size -= n;
                va += n;
                offset += n;
            }

            /* Map the section. We use the file content as the underlying storage of the map because we might be mapping parts of
             * the file left and right of the actual section. */
            if (mem_size>0) {
                trace <<"    Mapping section:                          va="
                      <<StringUtility::addrToString(va) <<" + " <<StringUtility::addrToString(mem_size) <<" = "
                      <<StringUtility::addrToString(va+mem_size) <<" "
                      <<(map_private?"private":"shared") <<"\n";
                if (map_private) {
                    map->insert(AddressInterval::baseSize(va, mem_size),
                                MemoryMap::Segment::anonymousInstance(mem_size, mapperms|MemoryMap::PRIVATE,
                                                                      melmt_name));
                    map->at(va).limit(mem_size).write(&file->get_data()[offset]);
                } else {
                    // Create the buffer, but the buffer should not take ownership of data from the file.
                    map->insert(AddressInterval::baseSize(va, mem_size),
                                MemoryMap::Segment(MemoryMap::StaticBuffer::instance(&file->get_data()[0],
                                                                                     file->get_data().size()),
                                                   offset, mapperms, melmt_name));
                }
            }

            if (trace) {
                trace <<"    After mapping this section:\n";
                map->dump(trace, "      ");
            }
        }
        header->set_base_va(old_base_va);
    } catch(...) {
        header->set_base_va(old_base_va);
        throw;
    }
}
Ejemplo n.º 12
0
int
main() 
{
    /* The SgAsmGenericFile is the root of a tree describing a binary file (executable, shared lib, object, core dump).
     * Usually this node appears in a much larger AST containing instructions contexts and other information, but for the
     * purposes of this example, we'll just be generating the ELF Container and not any instructions.  This root will
     * eventually be passed to unparse() to generate the executable file. */
    SgAsmGenericFile *ef = new SgAsmGenericFile;
    
    /* The ELF File Header is the first thing in the file, always at offset zero. The constructors generally take arguments to
     * describe the new object's relationship with existing objects, i.e., its location in the AST. In this case, the
     * SgAsmElfFileHeader is a child of the SgAsmGenericFile (file headers are always children of their respective file).
     *
     * Section constructors (SgAsmElfFileHeader derives from SgAsmGenericSection) always place the new section at the current
     * end-of-file and give it an initial size of one byte.  This ensures that each section has a unique starting address,
     * which is important when file memory is actually allocated for a section and other sections need to be moved out of the
     * way--the allocator needs to know the relative positions of the sections in order to correctly relocate them.
     *
     * If we were parsing an ELF file we would usually use ROSE's frontend() method. However, one can also parse the file by
     * first constructing the SgAsmElfFileHeader and then invoking its parse() method, which parses the ELF File Header and
     * everything that can be reached from that header.
     * 
     * We use 0x400000 as the virtual address of the main LOAD segment, which occupies the first part of the file, up through
     * the end of the .text section (see below). ELF Headers don't actually store a base address, so instead of assigning one
     * to the SgAsmElfFileHeader we'll leave the headers's base address at zero and add base_va explicitly whenever we need
     * it. */
    SgAsmElfFileHeader *fhdr = new SgAsmElfFileHeader(ef);
    fhdr->get_exec_format()->set_word_size(8);                  /* default is 32-bit; we want 64-bit */
    fhdr->set_isa(SgAsmExecutableFileFormat::ISA_X8664_Family); /* instruction set architecture; default is ISA_IA32_386 */
    rose_addr_t base_va = 0x400000;                             /* base virtual address */

    /* ELF executable files always have an ELF Segment Table (also called the ELF Program Header Table) usually appears
     * immediately after the ELF File Header.  The ELF Segment Table describes contiguous regions of the file that should be
     * memory mapped by the loader. ELF Segments don't have names--names are imparted to the segment by virtue of a segment
     * also being described by the ELF Section Table which we'll create later.
     *
     * Note that an SgAsmGenericSection (which represents both ELF Segments and ELF Sections) does not have the ELF Segment
     * Table or ELF Section Table as a parent in the AST, but rather has the ELF File Header as a parent. This allows a single
     * SgAsmGenericSection to represent an ELF Segment, an ELF Section, both, or neither. It also allows us to define ELF
     * Segments and ELF Sections without needing to first define the ELF Segment Table or ELF Section Table, which is
     * particularly useful when one wants one of those tables to appear at the end of the file. */
    SgAsmElfSegmentTable *segtab = new SgAsmElfSegmentTable(fhdr);

#if 1
    /* Create the .text section to hold machine instructions. We'll eventually add it to the ELF Section Table (which will be
     * created at the end of the file) and store its name in an ELF String Section associated with the ELF Section Table. The
     * fact that we haven't created the string table yet doesn't prevent us from giving the section a name.
     *
     * A note about names: Names are represented by SgAsmGenericString objects. The SgAsmGenericString class is virtual and
     * subclasses SgAsmBasicString and SgAsmStoredString represent strings that have no file storage and strings that are
     * stored in the file, respectively.  The stored-string classes are further specialized to handle string tables for
     * various file formats and understand how to map the string into a file, how to allocate and reallocate space, etc.  In
     * general, you want to change the string characters (with set_string()) stored in an SgAsmGenericString that's already
     * present in the AST.
     * 
     * The section constructors and reallocators don't worry about alignment issues--they always allocate from the next
     * available byte. However, instructions typically need to satisfy some alignment constraints. We can easily adjust the
     * file offset chosen by the constructor, but we also need to tell the reallocator about the alignment constraint.
     * 
     * NOTE: There's a slight nuance of the reallocator that must be accounted for if you want to be very precise about where
     *       things are located in the file. When the reallocator shifts sections to make room for a section that needs to
     *       grow, it shifts all affected sections by a uniform amount. This preserves overlaps (like when a segment overlaps
     *       with a bunch of sections) but it also means that the alignment constraint we'll set for ".text" will cause the
     *       ELF Segment Table constructed above at a lower offset to move in tandem with ".text" when the ELF File Header (at
     *       an even lower address) needs to grow to its final size.  This isn't a problem--you just end up with a couple unused
     *       bytes between the header and the segment table that will show up as a "hole" when subsequently parsed. If you're
     *       picky, the correct solution is to give the header a chance to allocate its file space before we construct the
     *       ".text" segment.
     *
     * In order to write the instructions out during the unparse phase, we create a new class from SgAsmElfSection and
     * override the virtual unparse() method.  The reallocate() method is called just before unparsing and we augment that in
     * order to set an ELF-specific bit in the ELF Section Table flags (alternatively, we could have set that bit after the
     * add_section() call below that adds the .text section to the ELF Section Table, but we do it here for demonstration).
     * The calculate_sizes() function is used by SgAsmElfSection::reallocate() to figure out how much space is needed for the
     * section.
     *
     * The instructions correspond to the assembly:
     *     .section .text
     *     .global _start
     * _start:
     *     movl    $1, %eax    # _exit(86)
     *     movl    $86, %ebx
     *     int     $0x80 */
    class TextSection : public SgAsmElfSection {
    public:
        TextSection(SgAsmElfFileHeader *fhdr, size_t ins_size, const unsigned char *ins_bytes)
            : SgAsmElfSection(fhdr), ins_size(ins_size), ins_bytes(ins_bytes)
            {}
        virtual rose_addr_t calculate_sizes(size_t *entsize, size_t *required, size_t *optional, size_t *entcount) const {
            if (entsize)  *entsize  = 1;                        /* an "entry" is one byte of instruction */
            if (required) *required = 1;                        /* each "entry" is also stored in one byte of the file */
            if (optional) *optional = 0;                        /* there is no extra data per instruction byte */
            if (entcount) *entcount = ins_size;                 /* number of "entries" is the total instruction bytes */
            return ins_size;                                    /* return value is section size required */
        }
        virtual bool reallocate() {
            bool retval = SgAsmElfSection::reallocate();        /* returns true if size or position of any section changed */
            SgAsmElfSectionTableEntry *ste = get_section_entry();
            ste->set_sh_flags(ste->get_sh_flags() | SgAsmElfSectionTableEntry::SHF_ALLOC); /* set the SHF_ALLOC bit */
            return retval;
        }
        virtual void unparse(std::ostream &f) const {
            write(f, 0, ins_size, ins_bytes);                   /* Write the instructions at offset zero in section */
        }
        size_t ins_size;
        const unsigned char *ins_bytes;
    };
    ef->reallocate();                                           /* Give existing sections a chance to allocate file space */
    static const unsigned char instructions[] = {0xb8, 0x01, 0x00, 0x00, 0x00, 0xbb, 0x56, 0x00, 0x00, 0x00, 0xcd, 0x80};
    SgAsmElfSection *text = new TextSection(fhdr, NELMTS(instructions), instructions);
    text->set_purpose(SgAsmGenericSection::SP_PROGRAM);         /* Program-supplied text/data/etc. */
    text->set_file_alignment(4);                                /* Tell reallocator about alignment constraint */
    text->set_mapped_alignment(4);                              /* Alignment constraint for memory mapping */
    text->set_mapped_preferred_rva(base_va+text->get_offset()); /* Mapped address is based on file offset */
    text->set_mapped_size(text->get_size());                    /* Mapped size is same as file size */
    text->set_mapped_rperm(true);                               /* Readable */
    text->set_mapped_wperm(false);                              /* Not writable */
    text->set_mapped_xperm(true);                               /* Executable */
    text->get_name()->set_string(".text");                      /* Give section the standard name */
    text->align();                                              /* Because we changed alignment constraints */
#else
    /* Another way to do the same thing without a temporary class. */
    ef->reallocate();
    static const unsigned char instructions[] = {0xb8, 0x01, 0x00, 0x00, 0x00, 0xbb, 0x56, 0x00, 0x00, 0x00, 0xcd, 0x80};
    SgAsmElfSection *text = new SgAsmElfSection(fhdr);
    text->set_purpose(SgAsmGenericSection::SP_PROGRAM);         /* Program-supplied text/data/etc. */
    text->set_file_alignment(4);                                /* Tell reallocator about alignment constraint */
    text->set_size(sizeof instructions);                        /* Give section a specific size */
    text->set_mapped_alignment(4);                              /* Alignment constraint for memory mapping */
    text->set_mapped_preferred_rva(base_va+text->get_offset()); /* Mapped address is based on file offset */
    text->set_mapped_size(text->get_size());                    /* Mapped size is same as file size */
    text->set_mapped_rperm(true);                               /* Readable */
    text->set_mapped_wperm(false);                              /* Not writable */
    text->set_mapped_xperm(true);                               /* Executable */
    text->get_name()->set_string(".text");                      /* Give section the standard name */
    text->align();                                              /* Because we changed alignment constraints */
    unsigned char *content = text->writable_content(sizeof instructions);
    memcpy(content, instructions, sizeof instructions);
#endif


    /* Set the main entry point to be the first virtual address in the text section. A rose_rva_t address is associated
     * with a section (.text in this case) so that if the starting address of the section changes (such as via reallocate())
     * then the rose_rva_t address will also change so as to continue to refer to the same byte of the section. */
    rose_rva_t entry_rva(text->get_mapped_preferred_rva(), text);
    fhdr->add_entry_rva(entry_rva);

    /* We now add an entry to the ELF Segment Table. ELF Segments often overlap multiple sections, and in this case we're
     * going to create a segment that overlaps with the ELF File Header, the ELF Segment Table, and the ".text" section. The
     * functions that reallocate space for sections (since most are born with a size of one byte) understand that things may
     * overlap and make appropriate adjustments. In this case all we need to do is set the starting offset and size. */
    SgAsmElfSection *seg1 = new SgAsmElfSection(fhdr);          /* ELF Segments are represented by SgAsmElfSection */
    seg1->get_name()->set_string("LOAD");                       /* Segment names aren't saved (but useful for debugging) */
    seg1->set_offset(0);                                        /* Starts at beginning of file */
    seg1->set_size(text->get_offset() + text->get_size());      /* Extends to end of .text section */
    seg1->set_mapped_preferred_rva(base_va);                    /* Typically mapped by loader to this memory address */
    seg1->set_mapped_size(seg1->get_size());                    /* Make mapped size match size in the file */
    seg1->set_mapped_rperm(true);                               /* Readable */
    seg1->set_mapped_wperm(false);                              /* Not writable */
    seg1->set_mapped_xperm(true);                               /* Executable */
    segtab->add_section(seg1);                                  /* Add definition to ELF Segment Table */

    /* Add an empty PAX segment as further demonstration of adding an ELF Segment. We need to tell the loader that this
     * section holds PAX Flags. There's no interface (currently) to do this at a generic level, so we make an adjustment
     * directly to the ELF Segment Table entry. */
    SgAsmElfSection *pax = new SgAsmElfSection(fhdr);
    pax->get_name()->set_string("PAX Flags");                   /* Name just for debugging */
    pax->set_offset(0);                                         /* Override address to be at zero rather than EOF */
    pax->set_size(0);                                           /* Override size to be zero rather than one byte */
    segtab->add_section(pax);                                   /* Add definition to ELF Segment Table */
    pax->get_segment_entry()->set_type(SgAsmElfSegmentTableEntry::PT_PAX_FLAGS);
    
    /* In order to give sections names there has to be an ELF String Section where the names are stored. An ELF String Section
     * is a section itself and will appear in the ELF Section Table. The name of this string section will be stored in a
     * string section, possibly this same string section. */
    SgAsmElfStringSection *shstrtab = new SgAsmElfStringSection(fhdr);
    shstrtab->get_name()->set_string(".shstrtab");

    /* Most files have an ELF Section Table that describes the various sections in the file (the code, data, dynamic linking,
     * symbol tables, string tables, etc). The ELF Section Table is a child of the ELF File Header, just as all other sections
     * reachable from the ELF File Header are also children of the header.  The class SgAsmGenericSection is used to represent
     * all types of contiguous file regions, whether they're ELF Sections, ELF Segments, PE Objects, etc.
     * 
     * The first entry of an ELF Section Table should always be an all-zero entry. It is not necessary to explicitly create
     * the zero entry since the unparse() method will do so. When parsing an ELF Section Table, the parser will read the
     * all-zero entry and actually create an empty, no-name, section with ID==0 (this is to handle cases that violate the ELF
     * specification).
     * 
     * Section names are stored in an ELF String Section that's associated with the ELF Section Table. We make that
     * association by adding the ELF String Section to the ELF Section Table. The first string table that's added will be the
     * one that stores section names. */
    SgAsmElfSectionTable *sectab = new SgAsmElfSectionTable(fhdr);
    sectab->add_section(text);                                  /* Add the .text section */
    sectab->add_section(shstrtab);                              /* Add the string table to store section names. */

#if 1 /* This stuff is here for demonstration. It's not necessary for the generated a.out to function properly. */

    /* Create a symbol table. Symbol tables should always be added to the ELF Section Table, and thus have a name. ELF Symbol
     * Tables need to know the section that serves as storage for the symbol names, an ELF String Section. The string table
     * for the section name (set above) does not need to be the same as the string table used for symbol names, although we
     * use the same one here just for brevity. */
    SgAsmElfSymbolSection *symtab = new SgAsmElfSymbolSection(fhdr, shstrtab);
    symtab->get_name()->set_string(".dynsym-test");
    symtab->set_is_dynamic(true);                               /* This is a symbol table for dynamic linking */
    sectab->add_section(symtab);                                /* Make the entry in the ELF Section Table */

    /* Add some symbols to the symbol table. Symbols are born with an empty name pointing into the symbol string table and the
     * names should be adjusted by calling get_name()->set_string("xxx"); there is no need to construct new SgAsmStoredString
     * objects and assign them with set_name(). */
    SgAsmElfSymbol *symbol;

    symbol = new SgAsmElfSymbol(symtab);                        /* New symbol is a child of the symbol table */
    symbol->set_value(0xaabbccdd);                              /* Arbitrary symbol value, often an RVA */
    symbol->set_size(512);                                      /* Arbitrary size of the thing to which the symbol refers */
    symbol->get_name()->set_string("symbolA");                  /* Symbol name */

    symbol = new SgAsmElfSymbol(symtab);
    symbol->set_value(0x11223344);
    symbol->set_size(4);
    symbol->get_name()->set_string("symbolB");

    /* Create a .dynamic section. Dynamic sections are generally added to both the section table and the segment table and are
     * memory mapped with read/write permission. They need an ELF String Table in order to store the library names. In real
     * executables the string table is usually separate from the others, but we'll just use the one we already created. */
    SgAsmElfDynamicSection *dynamic = new SgAsmElfDynamicSection(fhdr, shstrtab);
    dynamic->get_name()->set_string(".dynamic-test");           /* Give section a name */
    dynamic->set_mapped_preferred_rva(0x800000);                /* RVA where loader will map section */
    dynamic->set_mapped_size(dynamic->get_size());              /* Make mapped size same as file size */
    dynamic->set_mapped_rperm(true);                            /* Readable */
    dynamic->set_mapped_wperm(true);                            /* Writable */
    dynamic->set_mapped_xperm(false);                           /* Not executable */
    sectab->add_section(dynamic);                               /* Make an ELF Section Table entry */
    segtab->add_section(dynamic);                               /* Make an ELF Segment Table entry */

    /* Create some entries for the .dynamic section. Entries are born with a null name rather than an empty string since most
     * entries don't have a string value. So an entry that requires a string value (like DT_NEEDED) will need to have a new
     * string constructed. You don't have to worry about placing the new string in a particular string table since
     * reallocate() will eventually take care of that, so you can even use the very simple SgAsmBasicString interface. */
    SgAsmElfDynamicEntry *dynent;

    /* Add some DT_NULL entries */
    dynent = new SgAsmElfDynamicEntry(dynamic);
    dynent = new SgAsmElfDynamicEntry(dynamic);
    dynent = new SgAsmElfDynamicEntry(dynamic);

    /* Add a library name */
    dynent = new SgAsmElfDynamicEntry(dynamic);
    dynent->set_d_tag(SgAsmElfDynamicEntry::DT_NEEDED);
    dynent->set_name(new SgAsmBasicString("testlib"));

    /* Create a relocation table. Relocation tables depend on a symbol table and, indirectly, a string table. */
    SgAsmElfRelocSection *reladyn = new SgAsmElfRelocSection(fhdr, symtab,NULL);
    reladyn->get_name()->set_string(".rela.dyn-test");
    reladyn->set_uses_addend(true);                             /* This is a "rela" table rather than a "rel" table */
    sectab->add_section(reladyn);                               /* Make an ELF Section Table entry */

    /* Add a relocation entry to the relocation table. We create just one here, but in practice you would create as many as
     * you need.  Even REL tabels have a set_r_addend() method, but you should leave the addend at zero for them. The
     * relocation type determines how many bits of the offset are significant (and other details) and are described in the
     * system <elf.h> file (we may eventually change this interface to unify the various file formats). */
    SgAsmElfRelocEntry *relent = new SgAsmElfRelocEntry(reladyn);
    relent->set_r_offset(0x080495ac);                           /* An arbitrary offset */
    relent->set_r_addend(0xaa);                                 /* Arbitrary value to add to relocation address */
    relent->set_sym(symtab->index_of(symbol));                  /* Index of symbol in its symbol table */
    relent->set_type(SgAsmElfRelocEntry::R_386_JMP_SLOT);       /*  */

    /* We can add notes to an executable */
    SgAsmElfNoteSection *notes = new SgAsmElfNoteSection(fhdr);
    notes->set_name(new SgAsmBasicString("ELF Notes"));
    notes->set_mapped_preferred_rva(0x900000);                /* RVA where loader will map section */
    notes->set_mapped_size(notes->get_size());              /* Make mapped size same as file size */
    notes->set_mapped_rperm(true);                            /* Readable */
    notes->set_mapped_wperm(true);                            /* Writable */
    notes->set_mapped_xperm(false);                           /* Not executable */
    SgAsmElfNoteEntry *note = new SgAsmElfNoteEntry(notes);
    note->set_name(new SgAsmBasicString("ROSE"));
    const char *payload = "This is a test note.";
    for (size_t i=0; payload[i]; i++) {
        note->get_payload().push_back(payload[i]);
    }
    segtab->add_section(notes);


    /* Add a Symbol Version section.  This is a gnu extension to elf to support versioning of individual symbols 
       the size of the symver section should match the size of the dynamic symbol section (nominally .dynsym).
       there should also be an entry in the .dynamic section [DT_VERDEFNUM] that matches this same number.
     */
    SgAsmElfSymverSection* symver = new SgAsmElfSymverSection(fhdr);
    symver->get_name()->set_string(".gnu-version-test");       /* Give section a name */
    symver->set_mapped_preferred_rva(0xA00000);                /* RVA where loader will map section */
    symver->set_mapped_size(symver->get_size());               /* Make mapped size same as file size */
    symver->set_mapped_rperm(true);                            /* Readable */
    symver->set_mapped_wperm(false);                           /* Writable */
    symver->set_mapped_xperm(false);                           /* Not executable */
    sectab->add_section(symver);                               /* Make an ELF Section Table entry */

    for(size_t i=0; i < symtab->get_symbols()->get_symbols().size(); ++i){
      SgAsmElfSymverEntry* symver_entry = new SgAsmElfSymverEntry(symver);
      symver_entry->set_value(i);
    }
    
    SgAsmElfSymverDefinedSection* symver_def = new SgAsmElfSymverDefinedSection(fhdr,shstrtab);
    symver_def->get_name()->set_string(".gnu.version_d-test");
    symver_def->set_mapped_preferred_rva(0xB00000);                /* RVA where loader will map section */
    symver_def->set_mapped_size(symver_def->get_size());               /* Make mapped size same as file size */
    symver_def->set_mapped_rperm(true);                            /* Readable */
    symver_def->set_mapped_wperm(false);                           /* Writable */
    symver_def->set_mapped_xperm(false);                           /* Not executable */

    sectab->add_section(symver_def);                               /* Make an ELF Section Table entry */

    /** normal case - one Aux structure*/
    SgAsmElfSymverDefinedEntry* symdefEntry1 = new SgAsmElfSymverDefinedEntry(symver_def);
    symdefEntry1->set_version(1);
    symdefEntry1->set_hash(0xdeadbeef);// need to implement real hash support?
    symdefEntry1->set_flags(0x0);
    
    SgAsmElfSymverDefinedAux* symdefAux1a = new SgAsmElfSymverDefinedAux(symdefEntry1, symver_def);
    symdefAux1a->get_name()->set_string("version-1");

    SgAsmElfSymverDefinedEntry* symdefEntry2 = new SgAsmElfSymverDefinedEntry(symver_def);
    new SgAsmElfSymverDefinedAux(symdefEntry2, symver_def);
    new SgAsmElfSymverDefinedAux(symdefEntry2, symver_def);

    /* Very strange case, but technically legal - zero Aux */
    new SgAsmElfSymverDefinedEntry(symver_def);

    SgAsmElfSymverNeededSection* symver_need = new SgAsmElfSymverNeededSection(fhdr,shstrtab);
    symver_need->get_name()->set_string(".gnu.version_d-test");
    symver_need->set_mapped_preferred_rva(0xC00000);                /* RVA where loader will map section */
    symver_need->set_mapped_size(symver_need->get_size());               /* Make mapped size same as file size */
    symver_need->set_mapped_rperm(true);                            /* Readable */
    symver_need->set_mapped_wperm(false);                           /* Writable */
    symver_need->set_mapped_xperm(false);                           /* Not executable */
    sectab->add_section(symver_need);                               /* Make an ELF Section Table entry */


#endif

    /* Some of the sections we created above have default sizes of one byte because there's no way to determine their true
     * size until we're all done creating sections.  The SgAsmGenericFile::reallocate() traverses the AST and allocates the
     * actual file storage for each section, adjusting other sections to make room and updating various tables and
     * file-based data structures. You can call this function at any time, but it must be called before unparse(). */
    ef->reallocate();

    /* The reallocate() has a shortcoming [as of 2008-12-19] in that it might not correctly update memory mappings in the case
     * where the mapping for a section is inferred from the mapping of a containing segment when the section is shifted in the
     * file to make room for an earlier section that's also in the segment.
     * 
     * We can work around that here by explicitly setting the memory mapping to be relative to the segment. However, we'll
     * need to call reallocate() again because besides moving things around, reallocate() also updates some ELF-specific data
     * structures from values stored in the more generic structures. */
    text->set_mapped_preferred_rva(seg1->get_mapped_preferred_rva()+(text->get_offset()-seg1->get_offset()));
    ef->reallocate(); /*won't resize or move things this time since we didn't modify much since the last call to reallocate()*/

    /* Show some results. This is largely the same output as would be found in the *.dump file produced by many of the other
     * binary analysis tools. */
    ef->dump(stdout);
    SgAsmGenericSectionPtrList all = ef->get_sections(true);
    for (size_t i=0; i<all.size(); i++) {
        fprintf(stdout, "Section %zu:\n", i);
        all[i]->dump(stdout, "    ", -1);
    }

    /* Unparse the AST to generate an executable. */
    std::ofstream f("a.out");
    ef->unparse(f);

    return 0;
}