Merge change 3645

* changes:
  Make otcc code work in x64 based system with 32-bit chroot.
This commit is contained in:
Android (Google) Code Review 2009-06-09 16:01:04 -07:00
commit 607e5dd69b
6 changed files with 44 additions and 19 deletions

View File

@ -10,6 +10,7 @@
#include <ctype.h>
#include <dlfcn.h>
#include <errno.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdint.h>
@ -18,6 +19,10 @@
#include <string.h>
#include <cutils/hashmap.h>
#if defined(__i386__)
#include <sys/mman.h>
#endif
#if defined(__arm__)
#include <unistd.h>
#endif
@ -989,7 +994,14 @@ class Compiler : public ErrorSink {
}
virtual int finishCompile() {
return 0;
size_t pagesize = 4096;
size_t base = (size_t) getBase() & ~ (pagesize - 1);
size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1);
int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC);
if (err) {
error("mprotect() failed: %d", errno);
}
return err;
}
private:
@ -1031,7 +1043,7 @@ class Compiler : public ErrorSink {
void gmov(int l, int t) {
o(l + 0x83);
oad((t < LOCAL) << 7 | 5, t);
oad((t > -LOCAL && t < LOCAL) << 7 | 5, t);
}
};
@ -2165,7 +2177,7 @@ public:
inp();
next();
globalDeclarations();
pGen->finishCompile();
result = pGen->finishCompile();
}
return result;
}

View File

@ -50,7 +50,7 @@ void ad() {
o();
}
C = 0;
d = h;
d = h;
if (X()) {
E(32);
M = D;
@ -162,7 +162,7 @@ void Z(int e) {
void N(int j, int e) {
ae(j + 131);
s((e < 512) << 7 | 5, e);
s((e > -512 && e < 512) << 7 | 5, e);
}
void T (int j) {
@ -404,7 +404,7 @@ void ab (int j) {
v=v +4;
}
ad();
if( d == 44)ad();
if( d == 44)ad() ;
}
ad();
}
@ -432,7 +432,12 @@ void ab (int j) {
}
}
int run(int g, int e) {
return (*(int(*)()) *(int*) (P + 592))(g, e);
}
int main(int g, int e) {
int result;
Q = stdin;
if (g-- > 1) {
e = e + 4;
@ -445,5 +450,13 @@ int main(int g, int e) {
o();
ad();
ab(0);
return (*(int(*)()) *(int*) (P + 592))(g, e);
if (mprotect((ac + 592) & (~ 4095), (99999 + 4095) & (~ 4095), 7)) {
printf("Mprotect failed. %d\n", errno);
return -1;
}
fprintf(stderr, "otcc-ansi.c: About to execute compiled code:\n");
result = run(g, e);
fprintf(stderr, "atcc-ansi.c: result: %d\n", result);
return result;
}

View File

@ -441,6 +441,8 @@ P V;
o f;
c;
ab(0);
mprotect(ac & (~ 4095), (99999 + 4095) & (~ 4095), 7);
fprintf(stderr, "otcc.c: about to execute compiled code.\n");
J(*(int(*)f)k(P+592))(g,n;
}

View File

@ -1,10 +1,8 @@
#define VALUE (2*FOO)
#define FOO 12
int main(int argc, char** argv) {
return f();
}
int f() {
return VALUE;
return 42;
}

View File

@ -1,6 +1,3 @@
#pragma foo3(bar) //sdfsfd
#pragma a(b)
main() {
return 42;
}

View File

@ -2,17 +2,20 @@
SCRIPT_DIR=`dirname $BASH_SOURCE`
DATA=$SCRIPT_DIR/data
ACC=`which acc`
echo "Compiling returnval-ansi.c"
acc -S $DATA/returnval-ansi.c
$ACC -S $DATA/returnval-ansi.c
echo "Compiling whole compiler."
acc -S "$DATA/otcc-ansi.c"
$ACC -S "$DATA/otcc-ansi.c"
if (( "$(uname)" = "Linux" )) && (( "$(uname -m)" = "i686" )); then
echo "Linux i686. Testing otcc-ansi.c"
acc -R "$DATA/otcc-ansi.c" "$DATA/returnval.c"
acc -R $DATA/otcc-ansi.c $DATA/otcc.c $DATA/returnval.c
if file $ACC | grep -q "ELF 32-bit LSB executable, Intel 80386"; then
echo "Linux 32bit Intel."
$ACC -R $DATA/returnval-ansi.c
echo Testing otcc-ansi.c
$ACC -R "$DATA/otcc-ansi.c" "$DATA/returnval.c"
$ACC -R $DATA/otcc-ansi.c $DATA/otcc.c $DATA/returnval.c
fi
echo "Done with tests."