Retro68/Elf2Mac/Reloc.cc

77 lines
1.9 KiB
C++
Raw Normal View History

2017-09-28 23:43:54 +00:00
/*
Copyright 2017 Wolfgang Thaller.
2017-09-28 23:43:54 +00:00
This file is part of Retro68.
2017-09-28 23:43:54 +00:00
Retro68 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2017-09-28 23:43:54 +00:00
Retro68 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2017-09-28 23:43:54 +00:00
You should have received a copy of the GNU General Public License
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
2017-09-28 23:43:54 +00:00
*/
#include "Reloc.h"
#include <sstream>
#include "BinaryIO.h"
2017-09-28 23:43:54 +00:00
Reloc::Reloc()
{
}
Reloc::Reloc(const GElf_Rela &rela)
: GElf_Rela(rela), relocBase(RelocBase::code)
{
}
std::string SerializeRelocsUncompressed(std::vector<RuntimeReloc> relocs)
{
std::ostringstream out;
for(const auto& r : relocs)
{
longword(out, r.offset | ((int)r.base << 24));
}
longword(out, -1);
return out.str();
}
std::string SerializeRelocs(std::vector<RuntimeReloc> relocs)
{
std::ostringstream out;
2020-12-27 23:25:36 +00:00
for(int relative = 0; relative <= 1; relative++)
{
2020-12-27 23:25:36 +00:00
uint32_t offset = -1;
2020-12-27 23:25:36 +00:00
for(const auto& r : relocs)
{
if(r.relative == (bool)relative)
{
uint32_t delta = r.offset - offset;
offset = r.offset;
2020-12-27 23:25:36 +00:00
uint32_t base = (uint32_t) r.base;
2020-12-27 23:25:36 +00:00
uint32_t encoded = (delta << 2) | base;
while(encoded >= 128)
{
byte(out, (encoded & 0x7F) | 0x80);
encoded >>= 7;
}
byte(out, encoded);
}
}
2020-12-27 23:25:36 +00:00
byte(out, 0);
}
return out.str();
}