package jdk.tools.jaotc.binformat.macho;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import jdk.tools.jaotc.binformat.BinaryContainer;
import jdk.tools.jaotc.binformat.ByteContainer;
import jdk.tools.jaotc.binformat.CodeContainer;
import jdk.tools.jaotc.binformat.ReadOnlyDataContainer;
import jdk.tools.jaotc.binformat.Relocation;
import jdk.tools.jaotc.binformat.Relocation.RelocType;
import jdk.tools.jaotc.binformat.Symbol;
import jdk.tools.jaotc.binformat.Symbol.Kind;
import jdk.tools.jaotc.binformat.macho.MachO.section_64;
import jdk.tools.jaotc.binformat.macho.MachO.mach_header_64;
import jdk.tools.jaotc.binformat.macho.MachO.segment_command_64;
import jdk.tools.jaotc.binformat.macho.MachO.version_min_command;
import jdk.tools.jaotc.binformat.macho.MachO.symtab_command;
import jdk.tools.jaotc.binformat.macho.MachO.dysymtab_command;
import jdk.tools.jaotc.binformat.macho.MachO.nlist_64;
import jdk.tools.jaotc.binformat.macho.MachO.reloc_info;
import jdk.tools.jaotc.binformat.macho.MachOContainer;
import jdk.tools.jaotc.binformat.macho.MachOTargetInfo;
import jdk.tools.jaotc.binformat.macho.MachOSymtab;
import jdk.tools.jaotc.binformat.macho.MachORelocTable;
public class JMachORelocObject {
private final BinaryContainer binContainer;
private final MachOContainer machoContainer;
private final int segmentSize;
public JMachORelocObject(BinaryContainer binContainer, String outputFileName) {
this.binContainer = binContainer;
this.machoContainer = new MachOContainer(outputFileName);
this.segmentSize = binContainer.getCodeSegmentSize();
}
private void createByteSection(ArrayList<MachOSection> sections,
ByteContainer c, String sectName, String segName, int scnFlags) {
if (c.getByteArray().length == 0) {
}
MachOSection sect = new MachOSection(sectName,
segName,
c.getByteArray(),
scnFlags,
c.hasRelocations(),
segmentSize);
sections.add(sect);
c.setSectionId(sections.size() - 1);
}
private void createCodeSection(ArrayList<MachOSection> sections, CodeContainer c) {
createByteSection(sections, c, "__text", "__TEXT",
section_64.S_ATTR_PURE_INSTRUCTIONS |
section_64.S_ATTR_SOME_INSTRUCTIONS);
}
private void createReadOnlySection(ArrayList<MachOSection> sections, ReadOnlyDataContainer c) {
createByteSection(sections, c, c.getContainerName(), "__TEXT",
section_64.S_ATTR_SOME_INSTRUCTIONS);
}
private void createReadWriteSection(ArrayList<MachOSection> sections, ByteContainer c) {
createByteSection(sections, c, c.getContainerName(), "__DATA", section_64.S_REGULAR);
}
public void createMachORelocObject(Map<Symbol, List<Relocation>> relocationTable, Collection<Symbol> symbols) throws IOException {
MachOHeader mh = new MachOHeader();
ArrayList<MachOSection> sections = new ArrayList<>();
createCodeSection(sections, binContainer.getCodeContainer());
createReadOnlySection(sections, binContainer.getMetaspaceNamesContainer());
createReadOnlySection(sections, binContainer.getKlassesOffsetsContainer());
createReadOnlySection(sections, binContainer.getMethodsOffsetsContainer());
createReadOnlySection(sections, binContainer.getKlassesDependenciesContainer());
createReadOnlySection(sections, binContainer.getMethodMetadataContainer());
createReadOnlySection(sections, binContainer.getStubsOffsetsContainer());
createReadOnlySection(sections, binContainer.getHeaderContainer().getContainer());
createReadOnlySection(sections, binContainer.getCodeSegmentsContainer());
createReadOnlySection(sections, binContainer.getConstantDataContainer());
createReadOnlySection(sections, binContainer.getConfigContainer());
createReadWriteSection(sections, binContainer.getKlassesGotContainer());
createReadWriteSection(sections, binContainer.getCountersGotContainer());
createReadWriteSection(sections, binContainer.getMetadataGotContainer());
createReadWriteSection(sections, binContainer.getMethodStateContainer());
createReadWriteSection(sections, binContainer.getOopGotContainer());
createReadWriteSection(sections, binContainer.getExtLinkageGOTContainer());
mh.setCmdSizes(4, segment_command_64.totalsize +
(section_64.totalsize * sections.size()) +
version_min_command.totalsize +
symtab_command.totalsize +
dysymtab_command.totalsize);
int file_offset = mach_header_64.totalsize + mh.getCmdSize();
file_offset = (file_offset + (sections.get(0).getAlign() - 1)) & ~((sections.get(0).getAlign() - 1));
long address = 0;
int segment_offset = file_offset;
for (int i = 0; i < sections.size(); i++) {
MachOSection sect = sections.get(i);
file_offset = (file_offset + (sect.getAlign() - 1)) & ~((sect.getAlign() - 1));
address = (address + (sect.getAlign() - 1)) & ~((sect.getAlign() - 1));
sect.setOffset(file_offset);
sect.setAddr(address);
file_offset += sect.getSize();
address += sect.getSize();
}
int segment_size = file_offset - segment_offset;
MachOSegment seg = new MachOSegment(segment_command_64.totalsize +
(section_64.totalsize * sections.size()),
segment_offset,
segment_size,
sections.size());
MachOVersion vers = new MachOVersion();
MachOSymtab symtab = createMachOSymbolTables(sections, symbols);
MachODySymtab dysymtab = new MachODySymtab(symtab.getNumLocalSyms(),
symtab.getNumGlobalSyms(),
symtab.getNumUndefSyms());
MachORelocTable machORelocs = createMachORelocTable(sections, relocationTable, symtab);
file_offset = (file_offset + (MachORelocTable.getAlign() - 1)) & ~((MachORelocTable.getAlign() - 1));
for (int i = 0; i < sections.size(); i++) {
MachOSection sect = sections.get(i);
if (sect.hasRelocations()) {
int nreloc = machORelocs.getNumRelocs(i);
sect.setReloff(file_offset);
sect.setRelcount(nreloc);
file_offset += (nreloc * reloc_info.totalsize);
}
}
file_offset = (file_offset + (MachOSymtab.getAlign() - 1)) & ~((MachOSymtab.getAlign() - 1));
symtab.setOffset(file_offset);
machoContainer.writeBytes(mh.getArray());
machoContainer.writeBytes(seg.getArray());
for (int i = 0; i < sections.size(); i++) {
MachOSection sect = sections.get(i);
machoContainer.writeBytes(sect.getArray());
}
machoContainer.writeBytes(vers.getArray());
symtab.calcSizes();
machoContainer.writeBytes(symtab.getCmdArray());
machoContainer.writeBytes(dysymtab.getArray());
for (int i = 0; i < sections.size(); i++) {
MachOSection sect = sections.get(i);
machoContainer.writeBytes(sect.getDataArray(), sect.getAlign());
}
for (int i = 0; i < sections.size(); i++) {
if (machORelocs.getNumRelocs(i) > 0) {
machoContainer.writeBytes(machORelocs.getRelocData(i), MachORelocTable.getAlign());
}
}
machoContainer.writeBytes(symtab.getDataArray(), MachOSymtab.getAlign());
machoContainer.close();
}
private static MachOSymtab createMachOSymbolTables(ArrayList<MachOSection> sections,
Collection<Symbol> symbols) {
MachOSymtab symtab = new MachOSymtab();
symtab.addSymbolEntry("", (byte) nlist_64.N_UNDF, (byte) 0, 0);
for (Symbol symbol : symbols) {
int sectionId = symbol.getSection().getSectionId();
long sectionAddr = sections.get(sectionId).getAddr();
MachOSymbol machoSymbol = symtab.addSymbolEntry(symbol.getName(),
getMachOTypeOf(symbol),
(byte) sectionId,
symbol.getOffset() + sectionAddr);
symbol.setNativeSymbol(machoSymbol);
}
symtab.updateIndexes();
return (symtab);
}
private static byte getMachOTypeOf(Symbol sym) {
Kind kind = sym.getKind();
byte type = nlist_64.N_UNDF;
if (sym.getBinding() == Symbol.Binding.GLOBAL) {
type = nlist_64.N_EXT;
}
if (kind == Symbol.Kind.NATIVE_FUNCTION ||
kind == Symbol.Kind.JAVA_FUNCTION ||
kind == Symbol.Kind.OBJECT) {
type |= (nlist_64.N_SECT);
}
return (type);
}
private MachORelocTable createMachORelocTable(ArrayList<MachOSection> sections,
Map<Symbol, List<Relocation>> relocationTable,
MachOSymtab symtab) {
MachORelocTable machORelocTable = new MachORelocTable(sections.size());
for (Map.Entry<Symbol, List<Relocation>> entry : relocationTable.entrySet()) {
List<Relocation> relocs = entry.getValue();
Symbol symbol = entry.getKey();
for (Relocation reloc : relocs) {
createRelocation(symbol, reloc, machORelocTable);
}
}
for (Map.Entry<Symbol, Relocation> entry : binContainer.getUniqueRelocationTable().entrySet()) {
createRelocation(entry.getKey(), entry.getValue(), machORelocTable);
}
return (machORelocTable);
}
private static void createRelocation(Symbol symbol, Relocation reloc, MachORelocTable machORelocTable) {
RelocType relocType = reloc.getType();
int machORelocType = getMachORelocationType(relocType);
MachOSymbol sym = (MachOSymbol) symbol.getNativeSymbol();
int symno = sym.getIndex();
int sectindex = reloc.getSection().getSectionId();
int offset = reloc.getOffset();
int pcrel = 0;
int length = 0;
int isextern = 1;
switch (relocType) {
case JAVA_CALL_DIRECT:
case STUB_CALL_DIRECT:
case FOREIGN_CALL_INDIRECT_GOT: {
int addend = -4;
offset = offset + reloc.getSize() + addend;
pcrel = 1;
length = 2;
break;
}
case JAVA_CALL_INDIRECT: {
return;
}
case METASPACE_GOT_REFERENCE:
case EXTERNAL_PLT_TO_GOT: {
int addend = -4;
offset = offset + addend;
pcrel = 1;
length = 2;
break;
}
case EXTERNAL_GOT_TO_PLT: {
pcrel = 0;
length = 3;
break;
}
default:
throw new InternalError("Unhandled relocation type: " + relocType);
}
machORelocTable.createRelocationEntry(sectindex, offset, symno,
pcrel, length, isextern,
machORelocType);
}
private static int getMachORelocationType(RelocType relocType) {
int machORelocType = 0;
switch (MachOTargetInfo.getMachOArch()) {
case mach_header_64.CPU_TYPE_X86_64:
if (relocType == RelocType.JAVA_CALL_DIRECT ||
relocType == RelocType.FOREIGN_CALL_INDIRECT_GOT) {
machORelocType = reloc_info.X86_64_RELOC_BRANCH;
} else if (relocType == RelocType.STUB_CALL_DIRECT) {
machORelocType = reloc_info.X86_64_RELOC_BRANCH;
} else if (relocType == RelocType.JAVA_CALL_INDIRECT) {
machORelocType = reloc_info.X86_64_RELOC_NONE;
} else if (relocType == RelocType.METASPACE_GOT_REFERENCE ||
relocType == RelocType.EXTERNAL_PLT_TO_GOT) {
machORelocType = reloc_info.X86_64_RELOC_BRANCH;
} else if (relocType == RelocType.EXTERNAL_GOT_TO_PLT) {
machORelocType = reloc_info.X86_64_RELOC_UNSIGNED;
} else {
assert false : "Unhandled relocation type: " + relocType;
}
break;
default:
System.out.println("Relocation Type mapping: Unhandled architecture");
}
return machORelocType;
}
}