2014-12-22 12:35:05 +01:00
|
|
|
/*
|
|
|
|
** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
|
|
|
|
** save precompiled Lua chunks
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ldump_c
|
|
|
|
#define LUA_CORE
|
2015-09-01 07:11:36 +02:00
|
|
|
#define LUAC_CROSS_FILE
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
#include "lua.h"
|
2015-09-01 07:11:36 +02:00
|
|
|
#include C_HEADER_STRING
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lundump.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
lua_State* L;
|
|
|
|
lua_Writer writer;
|
|
|
|
void* data;
|
|
|
|
int strip;
|
|
|
|
int status;
|
|
|
|
DumpTargetInfo target;
|
|
|
|
size_t wrote;
|
|
|
|
} DumpState;
|
|
|
|
|
|
|
|
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
|
|
|
|
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpBlock(const void* b, size_t size, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
if (D->status==0)
|
|
|
|
{
|
|
|
|
lua_unlock(D->L);
|
|
|
|
D->status=(*D->writer)(D->L,b,size,D->data);
|
|
|
|
D->wrote+=size;
|
|
|
|
lua_lock(D->L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpChar(int y, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
char x=(char)y;
|
|
|
|
DumpVar(x,D);
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void Align4(DumpState *D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
while(D->wrote&3)
|
|
|
|
DumpChar(0,D);
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void MaybeByteSwap(char *number, size_t numbersize, DumpState *D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
int x=1;
|
|
|
|
int platform_little_endian = *(char*)&x;
|
|
|
|
if (platform_little_endian != D->target.little_endian)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
for (i=0; i<numbersize/2; i++)
|
|
|
|
{
|
|
|
|
char temp = number[i];
|
|
|
|
number[i] = number[numbersize-1-i];
|
|
|
|
number[numbersize-1-i] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpIntWithSize(int x, int sizeof_int, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
/* dump signed integer */
|
|
|
|
switch(sizeof_int) {
|
|
|
|
case 1: {
|
|
|
|
if (x>0x7F || x<(-0x80)) D->status=LUA_ERR_CC_INTOVERFLOW;
|
|
|
|
DumpChar(x,D);
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
if (x>0x7FFF || x<(-0x8000)) D->status=LUA_ERR_CC_INTOVERFLOW;
|
|
|
|
int16_t y=(int16_t)x;
|
|
|
|
MaybeByteSwap((char*)&y,2,D);
|
|
|
|
DumpVar(y,D);
|
|
|
|
} break;
|
|
|
|
case 4: {
|
|
|
|
/* Need to reduce bounds by 1 to avoid messing 32-bit compilers up */
|
|
|
|
if (x>0x7FFFFFFE || x<(-0x7FFFFFFF)) D->status=LUA_ERR_CC_INTOVERFLOW;
|
|
|
|
int32_t y=(int32_t)x;
|
|
|
|
MaybeByteSwap((char*)&y,4,D);
|
|
|
|
DumpVar(y,D);
|
|
|
|
} break;
|
|
|
|
default: lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpInt(int x, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
DumpIntWithSize(x,D->target.sizeof_int,D);
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpSize(uint32_t x, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
/* dump unsigned integer */
|
|
|
|
switch(D->target.sizeof_strsize_t) {
|
|
|
|
case 1: {
|
|
|
|
if (x>0xFF) D->status=LUA_ERR_CC_INTOVERFLOW;
|
|
|
|
DumpChar(x,D);
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
if (x>0xFFFF) D->status=LUA_ERR_CC_INTOVERFLOW;
|
|
|
|
uint16_t y=(uint16_t)x;
|
|
|
|
MaybeByteSwap((char*)&y,2,D);
|
|
|
|
DumpVar(y,D);
|
|
|
|
} break;
|
|
|
|
case 4: {
|
|
|
|
/* Reduce bounds to avoid messing 32-bit compilers up */
|
|
|
|
if (x>0xFFFFFFFE) D->status=LUA_ERR_CC_INTOVERFLOW;
|
|
|
|
uint32_t y=x;
|
|
|
|
MaybeByteSwap((char*)&y,4,D);
|
|
|
|
DumpVar(y,D);
|
|
|
|
} break;
|
|
|
|
default: lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpNumber(lua_Number x, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
#if defined( LUA_NUMBER_INTEGRAL ) && !defined( LUA_CROSS_COMPILER )
|
|
|
|
DumpIntWithSize(x,D->target.sizeof_lua_Number,D);
|
|
|
|
#else // #if defined( LUA_NUMBER_INTEGRAL ) && !defined( LUA_CROSS_COMPILER )
|
|
|
|
if (D->target.lua_Number_integral)
|
|
|
|
{
|
|
|
|
if (((float)(int)x)!=x) D->status=LUA_ERR_CC_NOTINTEGER;
|
|
|
|
DumpIntWithSize(x,D->target.sizeof_lua_Number,D);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(D->target.sizeof_lua_Number)
|
|
|
|
{
|
|
|
|
/* do we need bounds checking? */
|
|
|
|
case 4: {
|
|
|
|
float y=x;
|
|
|
|
MaybeByteSwap((char*)&y,4,D);
|
|
|
|
DumpVar(y,D);
|
|
|
|
} break;
|
|
|
|
case 8: {
|
|
|
|
double y=x;
|
|
|
|
// ARM FPA mode: keep endianness, but swap high and low parts of the
|
|
|
|
// memory representation. This is the default compilation mode for ARM
|
|
|
|
// targets with non-EABI gcc
|
|
|
|
if(D->target.is_arm_fpa)
|
|
|
|
{
|
|
|
|
char *pnum=(char*)&y, temp[4];
|
|
|
|
c_memcpy(temp,pnum,4);
|
|
|
|
c_memcpy(pnum,pnum+4,4);
|
|
|
|
c_memcpy(pnum+4,temp,4);
|
|
|
|
}
|
|
|
|
MaybeByteSwap((char*)&y,8,D);
|
|
|
|
DumpVar(y,D);
|
|
|
|
} break;
|
|
|
|
default: lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // #if defined( LUA_NUMBER_INTEGRAL ) && !defined( LUA_CROSS_COMPILER )
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpCode(const Proto *f, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
DumpInt(f->sizecode,D);
|
|
|
|
char buf[10];
|
|
|
|
int i;
|
|
|
|
Align4(D);
|
|
|
|
for (i=0; i<f->sizecode; i++)
|
|
|
|
{
|
|
|
|
c_memcpy(buf,&f->code[i],sizeof(Instruction));
|
|
|
|
MaybeByteSwap(buf,sizeof(Instruction),D);
|
|
|
|
DumpBlock(buf,sizeof(Instruction),D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpString(const TString* s, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
if (s==NULL || getstr(s)==NULL)
|
|
|
|
{
|
|
|
|
strsize_t size=0;
|
|
|
|
DumpSize(size,D);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strsize_t size=( strsize_t )s->tsv.len+1; /* include trailing '\0' */
|
|
|
|
DumpSize(size,D);
|
|
|
|
DumpBlock(getstr(s),size,D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpConstants(const Proto* f, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
int i,n=f->sizek;
|
|
|
|
DumpInt(n,D);
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
const TValue* o=&f->k[i];
|
|
|
|
DumpChar(ttype(o),D);
|
|
|
|
switch (ttype(o))
|
|
|
|
{
|
|
|
|
case LUA_TNIL:
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
DumpChar(bvalue(o),D);
|
|
|
|
break;
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
DumpNumber(nvalue(o),D);
|
|
|
|
break;
|
|
|
|
case LUA_TSTRING:
|
|
|
|
DumpString(rawtsvalue(o),D);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_assert(0); /* cannot happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n=f->sizep;
|
|
|
|
DumpInt(n,D);
|
|
|
|
for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpDebug(const Proto* f, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
int i,n;
|
|
|
|
n= (D->strip) ? 0 : f->sizelineinfo;
|
|
|
|
DumpInt(n,D);
|
|
|
|
Align4(D);
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
DumpInt(f->lineinfo[i],D);
|
|
|
|
}
|
|
|
|
|
|
|
|
n= (D->strip) ? 0 : f->sizelocvars;
|
|
|
|
DumpInt(n,D);
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
DumpString(f->locvars[i].varname,D);
|
|
|
|
DumpInt(f->locvars[i].startpc,D);
|
|
|
|
DumpInt(f->locvars[i].endpc,D);
|
|
|
|
}
|
|
|
|
|
|
|
|
n= (D->strip) ? 0 : f->sizeupvalues;
|
|
|
|
DumpInt(n,D);
|
|
|
|
for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
DumpString((f->source==p || D->strip) ? NULL : f->source,D);
|
|
|
|
DumpInt(f->linedefined,D);
|
|
|
|
DumpInt(f->lastlinedefined,D);
|
|
|
|
DumpChar(f->nups,D);
|
|
|
|
DumpChar(f->numparams,D);
|
|
|
|
DumpChar(f->is_vararg,D);
|
|
|
|
DumpChar(f->maxstacksize,D);
|
|
|
|
DumpCode(f,D);
|
|
|
|
DumpConstants(f,D);
|
|
|
|
DumpDebug(f,D);
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static void DumpHeader(DumpState* D)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
char buf[LUAC_HEADERSIZE];
|
|
|
|
char *h=buf;
|
|
|
|
|
|
|
|
/* This code must be kept in sync wiht luaU_header */
|
|
|
|
c_memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
|
|
|
|
h+=sizeof(LUA_SIGNATURE)-1;
|
|
|
|
*h++=(char)LUAC_VERSION;
|
|
|
|
*h++=(char)LUAC_FORMAT;
|
|
|
|
*h++=(char)D->target.little_endian;
|
|
|
|
*h++=(char)D->target.sizeof_int;
|
|
|
|
*h++=(char)D->target.sizeof_strsize_t;
|
|
|
|
*h++=(char)sizeof(Instruction);
|
|
|
|
*h++=(char)D->target.sizeof_lua_Number;
|
|
|
|
*h++=(char)D->target.lua_Number_integral;
|
|
|
|
|
|
|
|
DumpBlock(buf,LUAC_HEADERSIZE,D);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** dump Lua function as precompiled chunk with specified target
|
|
|
|
*/
|
2015-01-05 03:09:51 +01:00
|
|
|
int luaU_dump_crosscompile (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip, DumpTargetInfo target)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
DumpState D;
|
|
|
|
D.L=L;
|
|
|
|
D.writer=w;
|
|
|
|
D.data=data;
|
|
|
|
D.strip=strip;
|
|
|
|
D.status=0;
|
|
|
|
D.target=target;
|
|
|
|
D.wrote=0;
|
|
|
|
DumpHeader(&D);
|
|
|
|
DumpFunction(f,NULL,&D);
|
|
|
|
return D.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** dump Lua function as precompiled chunk with local machine as target
|
|
|
|
*/
|
2015-01-05 03:09:51 +01:00
|
|
|
int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
DumpTargetInfo target;
|
|
|
|
int test=1;
|
|
|
|
target.little_endian=*(char*)&test;
|
|
|
|
target.sizeof_int=sizeof(int);
|
|
|
|
target.sizeof_strsize_t=sizeof(strsize_t);
|
|
|
|
target.sizeof_lua_Number=sizeof(lua_Number);
|
|
|
|
target.lua_Number_integral=(((lua_Number)0.5)==0);
|
|
|
|
target.is_arm_fpa=0;
|
|
|
|
return luaU_dump_crosscompile(L,f,w,data,strip,target);
|
|
|
|
}
|