Facebook
From Sexy Butterfly, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 114
  1.  
  2. // segments - do zapisania
  3. // sections - sekcje tego samego typy (te same flagi)
  4. // rel_sections - wektor wszystkich sekcji z pliku rel
  5. // offset_map - mapa, index_sekcji -> offset sekcji
  6. // segments_flag - flagi
  7. void addNewSegment(Context& ctx, headerT& header,
  8.                    vector<segmentT>& segments,
  9.                    const vector<pair<int, sectionT>>& sections,
  10.                    vector<sectionT>& rel_sections,
  11.                    unordered_map<int, uint64_t>& offset_map,
  12.                    int segment_flags) {
  13.   if (sections.size()) {
  14.     segmentT p;
  15.     int size = 0;
  16.     int new_off = header.e_shoff;
  17.  
  18.     if (new_off % constants::kPageSize != 0) {
  19.       new_off += constants::kPageSize - (new_off % constants::kPageSize);
  20.     }
  21.     for (auto& s : sections) {
  22.         if (size % s.second.sh_addralign != 0) {
  23.           size += s.second.sh_addralign - (size % s.second.sh_addralign);
  24.         };
  25.         offset_map[s.first] = new_off + size;
  26.         size += s.second.sh_size;
  27.     }
  28.     if (size != 0) {
  29.       p.p_type = PT_LOAD;
  30.       p.p_flags = segment_flags;
  31.       p.p_offset = new_off;
  32.       p.p_vaddr = new_off + ctx.base_address;
  33.       p.p_paddr = new_off + ctx.base_address;
  34.       p.p_filesz = size;
  35.       p.p_memsz = size;
  36.       p.p_align = constants::kPageSize;
  37.  
  38.       ctx.last_segment += size;
  39.       segments.emplace_back(p);
  40.       header.e_phnum++;
  41.       header.e_shoff += size;
  42.     }
  43.   }
  44. }