2021-06-27 21:25:29 +00:00
|
|
|
/***
|
2022-04-20 13:03:51 +00:00
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
File: ModuleInfo.Linux.cpp
|
2022-04-20 13:03:51 +00:00
|
|
|
Date: 2022-4-20
|
2021-06-27 21:25:29 +00:00
|
|
|
Author: Reece
|
|
|
|
***/
|
2022-04-20 13:03:51 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessMap.Linux.hpp"
|
|
|
|
#include "AuProcessMap.hpp"
|
2022-04-20 23:34:08 +00:00
|
|
|
#include <link.h>
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_64BIT)
|
|
|
|
using Elf_Ehdr = Elf64_Ehdr;
|
|
|
|
using Elf_Shdr = Elf64_Shdr;
|
|
|
|
#else
|
|
|
|
using Elf_Ehdr = Elf32_Ehdr;
|
|
|
|
using Elf_Shdr = Elf32_Shdr;
|
|
|
|
#endif
|
2022-04-20 13:03:51 +00:00
|
|
|
|
2024-06-29 15:06:42 +00:00
|
|
|
namespace Aurora::CmdLine
|
|
|
|
{
|
|
|
|
extern AuString gHackLoader;
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:03:51 +00:00
|
|
|
namespace Aurora::Process
|
|
|
|
{
|
2024-06-29 15:06:42 +00:00
|
|
|
void ForceReplacePathHack(const AuString &path);
|
|
|
|
|
2022-04-20 13:03:51 +00:00
|
|
|
void InitProcessMapLinux()
|
|
|
|
{
|
2023-09-15 22:50:00 +00:00
|
|
|
|
2022-04-20 13:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitProcessMapLinux()
|
|
|
|
{
|
2023-09-15 22:50:00 +00:00
|
|
|
|
2022-04-20 13:03:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
static AuHashMap<AuUInt, AuTuple<AuString, AuList<AuTuple<AuUInt, AuUInt, AuUInt, AuUInt>>>> gModuleMap;
|
|
|
|
|
|
|
|
static int LdDlIIterateCallback(struct dl_phdr_info *info,
|
|
|
|
size_t size,
|
|
|
|
void *data)
|
2022-04-20 13:03:51 +00:00
|
|
|
{
|
2022-04-20 23:34:08 +00:00
|
|
|
AuString fileName = info->dlpi_name[0] ? info->dlpi_name : AuString{};
|
|
|
|
if (fileName.empty())
|
|
|
|
{
|
2024-02-23 12:44:43 +00:00
|
|
|
if (auto optProcessPath = GetProcessFullPath())
|
|
|
|
{
|
|
|
|
fileName = *optProcessPath;
|
|
|
|
}
|
2022-04-20 23:34:08 +00:00
|
|
|
}
|
2024-06-29 15:06:42 +00:00
|
|
|
else
|
2022-04-20 23:34:08 +00:00
|
|
|
{
|
2024-06-29 15:06:42 +00:00
|
|
|
if (!AuIOFS::FileExists(fileName))
|
|
|
|
{
|
|
|
|
// NO FILE linux-vdso.so.1
|
|
|
|
return 0;
|
|
|
|
}
|
2022-04-20 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuList<AuTuple<AuUInt, AuUInt, AuUInt, AuUInt>> sections;
|
|
|
|
for (int j = 0; j < info->dlpi_phnum; j++)
|
|
|
|
{
|
|
|
|
AuTryInsert(sections, AuMakeTuple((AuUInt)info->dlpi_phdr[j].p_vaddr, (AuUInt)info->dlpi_phdr[j].p_offset, (AuUInt)info->dlpi_phdr[j].p_memsz, (AuUInt)info->dlpi_phdr[j].p_flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
AuTryInsert(gModuleMap, info->dlpi_addr, AuMakeTuple(fileName, sections));
|
2022-04-20 13:03:51 +00:00
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PassOneScanLd()
|
|
|
|
{
|
|
|
|
::dl_iterate_phdr(LdDlIIterateCallback, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PassTwoScanMaps()
|
|
|
|
{
|
|
|
|
AuString map;
|
|
|
|
|
|
|
|
if (!AuIOFS::ReadString("/proc/self/maps", map))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-21 12:29:47 +00:00
|
|
|
Sections sections;
|
2024-05-07 13:41:16 +00:00
|
|
|
AuParse::SplitNewlines(map, [&](const AuROString &line)
|
2022-04-20 23:34:08 +00:00
|
|
|
{
|
2023-09-15 22:50:00 +00:00
|
|
|
bool bIsSpecialFile {};
|
2024-08-01 10:41:44 +00:00
|
|
|
const char *endPtr = line.data() + line.size();
|
|
|
|
const char *endPtr2 = line.data() + line.size();
|
|
|
|
const char *endPtr3 = line.data() + line.size();
|
|
|
|
auto optBase = AuParse::ParseUInt16(line.data(), endPtr);
|
|
|
|
if (!optBase) return;
|
|
|
|
auto base = optBase.value();
|
2022-04-20 23:34:08 +00:00
|
|
|
if (*endPtr != '-') return;
|
|
|
|
|
2024-08-01 10:41:44 +00:00
|
|
|
auto optEnd = AuParse::ParseUInt16(endPtr + 1, endPtr2);
|
|
|
|
if (!optEnd) return;
|
|
|
|
auto end = optEnd.value();
|
|
|
|
if (*endPtr2 != ' ') return;
|
2022-04-20 23:34:08 +00:00
|
|
|
|
2024-08-01 10:41:44 +00:00
|
|
|
auto perms = endPtr2 + 1;
|
2022-04-21 12:29:47 +00:00
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
auto A = line.find_first_of(':');
|
|
|
|
if (A == AuString::npos) return;
|
|
|
|
|
2023-09-15 22:50:00 +00:00
|
|
|
auto offsetStart = perms + 5;
|
2024-08-01 10:41:44 +00:00
|
|
|
auto optuFileOffset = AuParse::ParseUInt16(offsetStart, endPtr3);
|
|
|
|
AuUInt uFileOffset = optuFileOffset ? optuFileOffset.value() : 0u;
|
2023-09-15 22:50:00 +00:00
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
A += 4;
|
|
|
|
auto gross = line.substr(A);
|
|
|
|
|
|
|
|
AuString name;
|
|
|
|
auto B = gross.find_first_of(' ');
|
|
|
|
if (B != AuString::npos)
|
|
|
|
{
|
|
|
|
auto C = gross.find_first_not_of(' ', B);
|
|
|
|
if (C != AuString::npos)
|
|
|
|
{
|
|
|
|
name = gross.substr(C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "[stack]")
|
|
|
|
{
|
|
|
|
name = kSectionNameStack;
|
|
|
|
}
|
|
|
|
else if (name == "[heap]")
|
|
|
|
{
|
|
|
|
name = kSectionNameHeap;
|
|
|
|
}
|
|
|
|
else if (name == "[vdso]")
|
|
|
|
{
|
|
|
|
name = kSectionNameFile;
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:41:44 +00:00
|
|
|
AuOptional<AuROString> optProcessPath;
|
2024-06-29 15:06:42 +00:00
|
|
|
if (AuCmdLine::gHackLoader.size())
|
|
|
|
{
|
|
|
|
optProcessPath = GetProcessFullPath();
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
if (name.size())
|
|
|
|
{
|
2023-09-15 22:50:00 +00:00
|
|
|
bIsSpecialFile = AuIOFS::FileExists(name);
|
|
|
|
|
|
|
|
if (bIsSpecialFile)
|
2022-04-20 23:34:08 +00:00
|
|
|
{
|
2024-06-29 15:06:42 +00:00
|
|
|
for (auto &[uBaseAddress, pathSectionPair] : gModuleMap)
|
2023-09-15 22:50:00 +00:00
|
|
|
{
|
|
|
|
if (uBaseAddress == base)
|
|
|
|
{
|
2024-06-29 15:06:42 +00:00
|
|
|
if (optProcessPath)
|
|
|
|
{
|
|
|
|
if (AuGet<0>(pathSectionPair) == optProcessPath.Value())
|
|
|
|
{
|
|
|
|
ForceReplacePathHack(name);
|
|
|
|
AuGet<0>(pathSectionPair) = name;
|
|
|
|
AuResetMember(optProcessPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 22:50:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &[a, b, c, d] : AuGet<1>(pathSectionPair))
|
|
|
|
{
|
|
|
|
if ((uBaseAddress + a) <= base &&
|
|
|
|
(uBaseAddress + a + c) > base)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 23:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 12:29:47 +00:00
|
|
|
Section sect {};
|
|
|
|
|
|
|
|
sect.origVa = 0;
|
|
|
|
sect.baseVa = base;
|
|
|
|
sect.size = end - base;
|
2023-09-15 22:50:00 +00:00
|
|
|
sect.fsOff = uFileOffset;
|
2022-04-21 12:29:47 +00:00
|
|
|
|
|
|
|
sect.pt.readable = perms[0] == 'r';
|
|
|
|
sect.pt.writable = perms[1] == 'w';
|
|
|
|
sect.pt.NX = perms[2] != 'x';
|
2022-04-20 23:34:08 +00:00
|
|
|
|
2023-09-15 22:50:00 +00:00
|
|
|
if (bIsSpecialFile)
|
|
|
|
{
|
|
|
|
// edge case: these are like windows nt section views
|
|
|
|
// insert these as their own modules
|
|
|
|
// (we cant present Sections of weakly owned files )
|
|
|
|
static AuMutex gLock;
|
|
|
|
static AuHashMap<AuString, AuSPtr<PublicModule>> gMap;
|
|
|
|
|
|
|
|
sect.name = kSectionNameFile;
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(gLock);
|
|
|
|
|
|
|
|
auto &refMod = gMap[name];
|
|
|
|
if (refMod)
|
|
|
|
{
|
|
|
|
// already cached
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
refMod = AuMakeShared<PublicModule>();
|
|
|
|
if (!refMod) return;
|
|
|
|
|
|
|
|
refMod->moduleMeta = AuMakeShared<ModuleMeta>();
|
|
|
|
if (!refMod->moduleMeta) return;
|
|
|
|
|
2024-05-07 13:41:16 +00:00
|
|
|
AuROString fileName;
|
2023-09-15 22:50:00 +00:00
|
|
|
AuIOFS::GetFileFromPath(fileName, name);
|
|
|
|
refMod->moduleMeta->moduleBase = base;
|
|
|
|
refMod->moduleMeta->moduleName = fileName;
|
|
|
|
refMod->moduleMeta->modulePath = name;
|
|
|
|
refMod->moduleMeta->origVa = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sect.moduleMeta = refMod;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// most likely
|
|
|
|
sect.name = AuMove(name);
|
|
|
|
}
|
|
|
|
|
2022-04-21 12:29:47 +00:00
|
|
|
AuTryInsert(sections, sect);
|
2022-04-20 23:34:08 +00:00
|
|
|
}, false);
|
2022-04-21 12:29:47 +00:00
|
|
|
|
|
|
|
BorrowOtherSectionArray([&](AuList<Section> &out)
|
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
out.insert(out.end(), sections.begin(), sections.end());
|
|
|
|
});
|
2022-04-20 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PassThreeGatherMissingProgNames()
|
|
|
|
{
|
|
|
|
for (const auto & [baseAddress, pair] : gModuleMap)
|
|
|
|
{
|
|
|
|
auto & [path, sections] = pair;
|
|
|
|
|
2024-05-07 13:41:16 +00:00
|
|
|
AuROString file;
|
2022-04-20 23:34:08 +00:00
|
|
|
AuIOFS::GetFileFromPath(file, path);
|
|
|
|
auto object = AuIOFS::OpenReadUnique(path);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Elf_Ehdr header;
|
|
|
|
AuUInt read;
|
|
|
|
if (!object->Read(AuMemoryViewStreamWrite(AuMemoryViewWrite(&header, sizeof(header)), read)))
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
2023-08-11 15:51:42 +00:00
|
|
|
continue;
|
2022-04-20 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuList<Elf_Shdr> elfSections;
|
|
|
|
if (!AuTryResize(elfSections, header.e_shnum))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->SetOffset(header.e_shoff);
|
|
|
|
if (!object->Read(AuMemoryViewStreamWrite(AuMemoryViewWrite(elfSections.data(),
|
|
|
|
elfSections.size() * sizeof(Elf_Shdr)
|
|
|
|
),
|
|
|
|
read)))
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &strtab = elfSections[header.e_shstrndx];
|
|
|
|
|
|
|
|
AuList<AuUInt8> strHeap;
|
2023-08-11 15:51:42 +00:00
|
|
|
if (strtab.sh_size > 80 * 1024 * 1024)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
if (!AuTryResize(strHeap, strtab.sh_size))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->SetOffset(strtab.sh_offset);
|
|
|
|
if (!object->Read(AuMemoryViewStreamWrite(AuMemoryViewWrite(strHeap.data(),
|
2024-06-29 15:06:42 +00:00
|
|
|
strHeap.size()),
|
2022-04-20 23:34:08 +00:00
|
|
|
read)))
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sections modSections;
|
|
|
|
for (const auto & section : elfSections)
|
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
if (section.sh_name > strHeap.size())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
auto len = strnlen((const char *)strHeap.data() + section.sh_name, strHeap.size() - section.sh_name);
|
|
|
|
AuString sectionName((const char *)strHeap.data() + section.sh_name, len);
|
|
|
|
AuUInt fileOffset = section.sh_offset;
|
|
|
|
AuUInt programOffset = section.sh_addr;
|
|
|
|
|
|
|
|
Section sect {};
|
|
|
|
sect.origVa = section.sh_addr;
|
|
|
|
sect.baseVa = 0;
|
|
|
|
|
|
|
|
for (const auto & [base, offset, size, flags] : sections)
|
|
|
|
{
|
|
|
|
if ((offset <= fileOffset) &&
|
|
|
|
((offset + size) > fileOffset) && fileOffset && offset)
|
|
|
|
{
|
|
|
|
sect.pt.readable = section.sh_addr ? (section.sh_flags & SHF_ALLOC) : false;//?
|
|
|
|
|
|
|
|
auto baseOfMap = baseAddress;
|
|
|
|
auto offsetInMap = fileOffset - offset;
|
|
|
|
|
|
|
|
sect.baseVa = (baseOfMap + base) + offsetInMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sect.size = section.sh_size;
|
|
|
|
sect.fsOff = fileOffset;
|
|
|
|
|
|
|
|
sect.pt.NX = !(section.sh_flags & SHF_EXECINSTR);
|
|
|
|
sect.pt.writable = section.sh_flags & SHF_WRITE;
|
|
|
|
|
|
|
|
if (!sect.pt.readable)
|
|
|
|
{
|
|
|
|
sect.baseVa = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sect.name = sectionName;
|
|
|
|
|
2022-04-21 12:29:47 +00:00
|
|
|
if (sect.fsOff || sect.size)
|
|
|
|
{
|
|
|
|
AuTryInsert(modSections, sect);
|
|
|
|
}
|
2022-04-20 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pub = AuMakeShared<PublicModule>();
|
|
|
|
if (!pub) return;
|
|
|
|
|
|
|
|
pub->moduleMeta = AuMakeShared<ModuleMeta>();
|
|
|
|
if (!pub->moduleMeta) return;
|
|
|
|
|
|
|
|
pub->moduleMeta->moduleBase = baseAddress;
|
|
|
|
pub->moduleMeta->moduleName = AuMove(file);
|
|
|
|
pub->moduleMeta->modulePath = AuMove(path);
|
|
|
|
pub->moduleMeta->origVa = 0;
|
|
|
|
|
|
|
|
pub->sections = AuMove(modSections);
|
|
|
|
|
|
|
|
// TODO: ...
|
|
|
|
InsertModuleCache(ModuleBasePair{pub->moduleMeta->moduleName, baseAddress + AuGet<0>(sections[0])}, pub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PassFourAdd()
|
|
|
|
{
|
|
|
|
// TODO: ?
|
2022-04-20 13:03:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 23:34:08 +00:00
|
|
|
void RescanMaps()
|
|
|
|
{
|
|
|
|
PassOneScanLd();
|
|
|
|
PassTwoScanMaps();
|
|
|
|
PassThreeGatherMissingProgNames();
|
|
|
|
PassFourAdd();
|
|
|
|
}
|
2022-04-20 13:03:51 +00:00
|
|
|
}
|