Skip to content

Commit

Permalink
use slice instead of ptr/length in lib*.d
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and dlang-bot committed Mar 3, 2024
1 parent 9da69ee commit 02d6d07
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 89 deletions.
42 changes: 19 additions & 23 deletions compiler/src/dmd/libelf.d
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ final class LibElf : Library
* If the buffer is NULL, use module_name as the file name
* and load the file.
*/
override void addObject(const(char)[] module_name, const ubyte[] buffer)
override void addObject(const(char)[] module_name, const(ubyte)[] buffer)
{
static if (LOG)
{
Expand All @@ -98,55 +98,51 @@ final class LibElf : Library
}

int fromfile = 0;
auto buf = buffer.ptr;
auto buflen = buffer.length;
if (!buf)
if (!buffer.length)
{
assert(module_name.length);
// read file and take buffer ownership
Buffer b;
if (readFile(Loc.initial, module_name, b))
fatal();
auto data = b.extractSlice();
buf = data.ptr;
buflen = data.length;
buffer = b.extractSlice();
fromfile = 1;
}
if (buflen < 16)
if (buffer.length < 16)
{
static if (LOG)
{
printf("buf = %p, buflen = %d\n", buf, buflen);
printf("buf = %p, buffer.length = %d\n", buffer.ptr, buffer.length);
}
return corrupt(__LINE__);
}
if (memcmp(buf, "!<arch>\n".ptr, 8) == 0)
if (memcmp(buffer.ptr, "!<arch>\n".ptr, 8) == 0)
{
/* Library file.
* Pull each object module out of the library and add it
* to the object module array.
*/
static if (LOG)
{
printf("archive, buf = %p, buflen = %d\n", buf, buflen);
printf("archive, buf = %p, buffer.length = %d\n", buffer.ptr, buffer.length);
}
uint offset = 8;
char* symtab = null;
uint symtab_size = 0;
char* filenametab = null;
uint filenametab_size = 0;
uint mstart = cast(uint)objmodules.length;
while (offset < buflen)
while (offset < buffer.length)
{
if (offset + ElfLibHeader.sizeof >= buflen)
if (offset + ElfLibHeader.sizeof >= buffer.length)
return corrupt(__LINE__);
ElfLibHeader* header = cast(ElfLibHeader*)(cast(ubyte*)buf + offset);
ElfLibHeader* header = cast(ElfLibHeader*)(cast(ubyte*)buffer.ptr + offset);
offset += ElfLibHeader.sizeof;
char* endptr = null;
uint size = cast(uint)strtoul(header.file_size.ptr, &endptr, 10);
if (endptr >= header.file_size.ptr + 10 || *endptr != ' ')
return corrupt(__LINE__);
if (offset + size > buflen)
if (offset + size > buffer.length)
return corrupt(__LINE__);
if (header.object_name[0] == '/' && header.object_name[1] == ' ')
{
Expand All @@ -155,7 +151,7 @@ final class LibElf : Library
*/
if (symtab)
return corrupt(__LINE__);
symtab = cast(char*)buf + offset;
symtab = cast(char*)buffer.ptr + offset;
symtab_size = size;
if (size < 4)
return corrupt(__LINE__);
Expand All @@ -166,13 +162,13 @@ final class LibElf : Library
*/
if (filenametab)
return corrupt(__LINE__);
filenametab = cast(char*)buf + offset;
filenametab = cast(char*)buffer.ptr + offset;
filenametab_size = size;
}
else
{
auto om = new ElfObjModule();
om.base = cast(ubyte*)buf + offset; /*- sizeof(ElfLibHeader)*/
om.base = cast(ubyte*)buffer.ptr + offset; /*- sizeof(ElfLibHeader)*/
om.length = size;
om.offset = 0;
if (header.object_name[0] == '/')
Expand Down Expand Up @@ -223,7 +219,7 @@ final class LibElf : Library
}
offset += (size + 1) & ~1;
}
if (offset != buflen)
if (offset != buffer.length)
return corrupt(__LINE__);
/* Scan the library's symbol table, and insert it into our own.
* We use this instead of rescanning the object module, because
Expand All @@ -248,8 +244,8 @@ final class LibElf : Library
if (m == objmodules.length)
return corrupt(__LINE__); // didn't find it
ElfObjModule* om = objmodules[m];
//printf("\t%x\n", cast(char *)om.base - cast(char *)buf);
if (moff + ElfLibHeader.sizeof == cast(char*)om.base - cast(char*)buf)
//printf("\t%x\n", cast(char *)om.base - cast(char *)buffer.ptr);
if (moff + ElfLibHeader.sizeof == cast(char*)om.base - cast(char*)buffer.ptr)
{
addSymbol(om, name, 1);
//if (mstart == m)
Expand All @@ -263,8 +259,8 @@ final class LibElf : Library
/* It's an object module
*/
auto om = new ElfObjModule();
om.base = cast(ubyte*)buf;
om.length = cast(uint)buflen;
om.base = cast(ubyte*)buffer.ptr;
om.length = cast(uint)buffer.length;
om.offset = 0;
// remove path, but not extension
om.name = FileName.name(module_name);
Expand Down
40 changes: 18 additions & 22 deletions compiler/src/dmd/libmach.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ final class LibMach : Library
* If the buffer is NULL, use module_name as the file name
* and load the file.
*/
override void addObject(const(char)[] module_name, const ubyte[] buffer)
override void addObject(const(char)[] module_name, const(ubyte)[] buffer)
{
static if (LOG)
{
Expand All @@ -99,53 +99,49 @@ final class LibMach : Library
}

int fromfile = 0;
auto buf = buffer.ptr;
auto buflen = buffer.length;
if (!buf)
if (!buffer.length)
{
assert(module_name[0]);
// read file and take buffer ownership
Buffer b;
if (readFile(Loc.initial, module_name, b))
fatal();
auto data = b.extractSlice();
buf = data.ptr;
buflen = data.length;
buffer = b.extractSlice();
fromfile = 1;
}
if (buflen < 16)
if (buffer.length < 16)
{
static if (LOG)
{
printf("buf = %p, buflen = %d\n", buf, buflen);
printf("buf = %p, buffer.length = %d\n", buffer.ptr, buffer.length);
}
return corrupt(__LINE__);
}
if (memcmp(buf, "!<arch>\n".ptr, 8) == 0)
if (memcmp(buffer.ptr, "!<arch>\n".ptr, 8) == 0)
{
/* Library file.
* Pull each object module out of the library and add it
* to the object module array.
*/
static if (LOG)
{
printf("archive, buf = %p, buflen = %d\n", buf, buflen);
printf("archive, buf = %p, buffer.length = %d\n", buffer.ptr, buffer.length);
}
uint offset = 8;
char* symtab = null;
uint symtab_size = 0;
uint mstart = cast(uint)objmodules.length;
while (offset < buflen)
while (offset < buffer.length)
{
if (offset + MachLibHeader.sizeof >= buflen)
if (offset + MachLibHeader.sizeof >= buffer.length)
return corrupt(__LINE__);
MachLibHeader* header = cast(MachLibHeader*)(cast(ubyte*)buf + offset);
MachLibHeader* header = cast(MachLibHeader*)(cast(ubyte*)buffer.ptr + offset);
offset += MachLibHeader.sizeof;
char* endptr = null;
uint size = cast(uint)strtoul(header.file_size.ptr, &endptr, 10);
if (endptr >= header.file_size.ptr + 10 || *endptr != ' ')
return corrupt(__LINE__);
if (offset + size > buflen)
if (offset + size > buffer.length)
return corrupt(__LINE__);
if (memcmp(header.object_name.ptr, "__.SYMDEF ".ptr, 16) == 0 ||
memcmp(header.object_name.ptr, "__.SYMDEF SORTED".ptr, 16) == 0)
Expand All @@ -155,15 +151,15 @@ final class LibMach : Library
*/
if (symtab)
return corrupt(__LINE__);
symtab = cast(char*)buf + offset;
symtab = cast(char*)buffer.ptr + offset;
symtab_size = size;
if (size < 4)
return corrupt(__LINE__);
}
else
{
auto om = new MachObjModule();
om.base = cast(ubyte*)buf + offset - MachLibHeader.sizeof;
om.base = cast(ubyte*)buffer.ptr + offset - MachLibHeader.sizeof;
om.length = cast(uint)(size + MachLibHeader.sizeof);
om.offset = 0;
const n = cast(const(char)*)(om.base + MachLibHeader.sizeof);
Expand All @@ -177,7 +173,7 @@ final class LibMach : Library
}
offset += (size + 1) & ~1;
}
if (offset != buflen)
if (offset != buffer.length)
return corrupt(__LINE__);
/* Scan the library's symbol table, and insert it into our own.
* We use this instead of rescanning the object module, because
Expand All @@ -204,8 +200,8 @@ final class LibMach : Library
if (m == objmodules.length)
return corrupt(__LINE__); // didn't find it
MachObjModule* om = objmodules[m];
//printf("\tom offset = x%x\n", cast(char *)om.base - cast(char *)buf);
if (moff == cast(char*)om.base - cast(char*)buf)
//printf("\tom offset = x%x\n", cast(char *)om.base - cast(char *)buffer.ptr);
if (moff == cast(char*)om.base - cast(char*)buffer.ptr)
{
addSymbol(om, name[0 .. namelen], 1);
//if (mstart == m)
Expand All @@ -219,8 +215,8 @@ final class LibMach : Library
/* It's an object module
*/
auto om = new MachObjModule();
om.base = cast(ubyte*)buf;
om.length = cast(uint)buflen;
om.base = cast(ubyte*)buffer.ptr;
om.length = cast(uint)buffer.length;
om.offset = 0;
const n = FileName.name(module_name); // remove path, but not extension
om.name = n;
Expand Down
50 changes: 23 additions & 27 deletions compiler/src/dmd/libmscoff.d
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class LibMSCoff : Library
* If the buffer is NULL, use module_name as the file name
* and load the file.
*/
override void addObject(const(char)[] module_name, const ubyte[] buffer)
override void addObject(const(char)[] module_name, const(ubyte)[] buffer)
{
static if (LOG)
{
Expand All @@ -106,37 +106,33 @@ final class LibMSCoff : Library
}

int fromfile = 0;
auto buf = buffer.ptr;
auto buflen = buffer.length;
if (!buf)
if (!buffer.length)
{
assert(module_name.length, "No module nor buffer provided to `addObject`");
// read file and take buffer ownership
Buffer b;
if (readFile(Loc.initial, module_name, b))
fatal();
auto data = b.extractSlice();
buf = data.ptr;
buflen = data.length;
buffer = b.extractSlice();
fromfile = 1;
}
if (buflen < 16)
if (buffer.length < 16)
{
static if (LOG)
{
printf("buf = %p, buflen = %d\n", buf, buflen);
printf("buf = %p, buffer.length = %d\n", buffer.ptr, buffer.length);
}
return corrupt(__LINE__);
}
if (memcmp(buf, "!<arch>\n".ptr, 8) == 0)
if (memcmp(buffer.ptr, "!<arch>\n".ptr, 8) == 0)
{
/* It's a library file.
* Pull each object module out of the library and add it
* to the object module array.
*/
static if (LOG)
{
printf("archive, buf = %p, buflen = %d\n", buf, buflen);
printf("archive, buf = %p, buffer.length = %d\n", buffer.ptr, buffer.length);
}
MSCoffLibHeader* flm = null; // first linker member
MSCoffLibHeader* slm = null; // second linker member
Expand All @@ -154,17 +150,17 @@ final class LibMSCoff : Library
while (1)
{
offset = (offset + 1) & ~1; // round to even boundary
if (offset >= buflen)
if (offset >= buffer.length)
break;
if (offset + MSCoffLibHeader.sizeof >= buflen)
if (offset + MSCoffLibHeader.sizeof >= buffer.length)
return corrupt(__LINE__);
MSCoffLibHeader* header = cast(MSCoffLibHeader*)(cast(ubyte*)buf + offset);
MSCoffLibHeader* header = cast(MSCoffLibHeader*)(cast(ubyte*)buffer.ptr + offset);
offset += MSCoffLibHeader.sizeof;
char* endptr = null;
uint size = cast(uint)strtoul(cast(char*)header.file_size, &endptr, 10);
if (endptr >= header.file_size.ptr + 10 || *endptr != ' ')
return corrupt(__LINE__);
if (offset + size > buflen)
if (offset + size > buffer.length)
return corrupt(__LINE__);
//printf("header.object_name = '%.*s'\n", cast(int)MSCOFF_OBJECT_NAME_SIZE, header.object_name.ptr);
if (memcmp(cast(char*)header.object_name, cast(char*)"/ ", MSCOFF_OBJECT_NAME_SIZE) == 0)
Expand All @@ -180,13 +176,13 @@ final class LibMSCoff : Library
slm = header;
if (size < 4 + 4)
return corrupt(__LINE__);
number_of_members = Port.readlongLE(cast(char*)buf + offset);
member_file_offsets = cast(uint*)(cast(char*)buf + offset + 4);
number_of_members = Port.readlongLE(cast(char*)buffer.ptr + offset);
member_file_offsets = cast(uint*)(cast(char*)buffer.ptr + offset + 4);
if (size < 4 + number_of_members * 4 + 4)
return corrupt(__LINE__);
number_of_symbols = Port.readlongLE(cast(char*)buf + offset + 4 + number_of_members * 4);
indices = cast(ushort*)(cast(char*)buf + offset + 4 + number_of_members * 4 + 4);
string_table = cast(char*)(cast(char*)buf + offset + 4 + number_of_members * 4 + 4 + number_of_symbols * 2);
number_of_symbols = Port.readlongLE(cast(char*)buffer.ptr + offset + 4 + number_of_members * 4);
indices = cast(ushort*)(cast(char*)buffer.ptr + offset + 4 + number_of_members * 4 + 4);
string_table = cast(char*)(cast(char*)buffer.ptr + offset + 4 + number_of_members * 4 + 4 + number_of_symbols * 2);
if (size <= (4 + number_of_members * 4 + 4 + number_of_symbols * 2))
return corrupt(__LINE__);
string_table_length = size - (4 + number_of_members * 4 + 4 + number_of_symbols * 2);
Expand All @@ -213,7 +209,7 @@ final class LibMSCoff : Library
if (!lnm)
{
lnm = header;
longnames = cast(char*)buf + offset;
longnames = cast(char*)buffer.ptr + offset;
longnames_length = size;
}
}
Expand All @@ -229,7 +225,7 @@ final class LibMSCoff : Library
}
auto om = new MSCoffObjModule();
// Include MSCoffLibHeader in base[0..length], so we don't have to repro it
om.base = cast(ubyte*)buf + offset - MSCoffLibHeader.sizeof;
om.base = cast(ubyte*)buffer.ptr + offset - MSCoffLibHeader.sizeof;
om.length = cast(uint)(size + MSCoffLibHeader.sizeof);
om.offset = 0;
if (header.object_name[0] == '/')
Expand Down Expand Up @@ -281,7 +277,7 @@ final class LibMSCoff : Library
}
offset += size;
}
if (offset != buflen)
if (offset != buffer.length)
return corrupt(__LINE__);
/* Scan the library's symbol table, and insert it into our own.
* We use this instead of rescanning the object module, because
Expand All @@ -305,8 +301,8 @@ final class LibMSCoff : Library
if (m == objmodules.length)
return corrupt(__LINE__); // didn't find it
MSCoffObjModule* om = objmodules[m];
//printf("\tom offset = x%x\n", cast(char *)om.base - cast(char *)buf);
if (moff == cast(char*)om.base - cast(char*)buf)
//printf("\tom offset = x%x\n", cast(char *)om.base - cast(char *)buffer.ptr);
if (moff == cast(char*)om.base - cast(char*)buffer.ptr)
{
addSymbol(om, name, 1);
//if (mstart == m)
Expand All @@ -320,8 +316,8 @@ final class LibMSCoff : Library
/* It's an object module
*/
auto om = new MSCoffObjModule();
om.base = cast(ubyte*)buf;
om.length = cast(uint)buflen;
om.base = cast(ubyte*)buffer.ptr;
om.length = cast(uint)buffer.length;
om.offset = 0;
// remove path, but not extension
om.name = global.params.preservePaths ? module_name : FileName.name(module_name);
Expand Down
Loading

0 comments on commit 02d6d07

Please sign in to comment.