~ubuntu-branches/ubuntu/trusty/luajit/trusty

« back to all changes in this revision

Viewing changes to src/host/buildvm_peobj.c

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2013-05-20 18:08:12 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20130520180812-h514ggtgxg3nqmxl
Tags: 2.0.1+hotfix1+dfsg-1
* New upsream release
* Packaging moved to git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
** LuaJIT VM builder: PE object emitter.
3
 
** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
 
3
** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
4
4
**
5
5
** Only used for building on Windows, since we cannot assume the presence
6
6
** of a suitable assembler. The host and target byte order must match.
9
9
#include "buildvm.h"
10
10
#include "lj_bc.h"
11
11
 
12
 
#if LJ_TARGET_X86ORX64
 
12
#if LJ_TARGET_X86ORX64 || LJ_TARGET_PPC
13
13
 
14
14
/* Context for PE object emitter. */
15
15
static char *strtab;
84
84
#define PEOBJ_ARCH_TARGET       0x014c
85
85
#define PEOBJ_RELOC_REL32       0x14  /* MS: REL32, GNU: DISP32. */
86
86
#define PEOBJ_RELOC_DIR32       0x06
 
87
#define PEOBJ_RELOC_OFS         0
 
88
#define PEOBJ_TEXT_FLAGS        0x60500020  /* 60=r+x, 50=align16, 20=code. */
87
89
#elif LJ_TARGET_X64
88
90
#define PEOBJ_ARCH_TARGET       0x8664
89
91
#define PEOBJ_RELOC_REL32       0x04  /* MS: REL32, GNU: DISP32. */
90
92
#define PEOBJ_RELOC_DIR32       0x02
91
93
#define PEOBJ_RELOC_ADDR32NB    0x03
 
94
#define PEOBJ_RELOC_OFS         0
 
95
#define PEOBJ_TEXT_FLAGS        0x60500020  /* 60=r+x, 50=align16, 20=code. */
 
96
#elif LJ_TARGET_PPC
 
97
#define PEOBJ_ARCH_TARGET       0x01f2
 
98
#define PEOBJ_RELOC_REL32       0x06
 
99
#define PEOBJ_RELOC_DIR32       0x02
 
100
#define PEOBJ_RELOC_OFS         (-4)
 
101
#define PEOBJ_TEXT_FLAGS        0x60400020  /* 60=r+x, 40=align8, 20=code. */
92
102
#endif
93
103
 
94
104
/* Section numbers (0-based). */
170
180
  int i, nrsym;
171
181
  union { uint8_t b; uint32_t u; } host_endian;
172
182
 
173
 
  host_endian.u = 1;
174
 
  if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) {
175
 
    fprintf(stderr, "Error: different byte order for host and target\n");
176
 
    exit(1);
177
 
  }
178
 
 
179
183
  sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection);
180
184
 
181
185
  /* Fill in PE sections. */
186
190
  pesect[PEOBJ_SECT_TEXT].relocofs = sofs;
187
191
  sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE;
188
192
  /* Flags: 60 = read+execute, 50 = align16, 20 = code. */
189
 
  pesect[PEOBJ_SECT_TEXT].flags = 0x60500020;
 
193
  pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS;
190
194
 
191
195
#if LJ_TARGET_X64
192
196
  memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1);
236
240
  owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS);
237
241
 
238
242
  /* Write .text section. */
 
243
  host_endian.u = 1;
 
244
  if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) {
 
245
#if LJ_TARGET_PPC
 
246
    uint32_t *p = (uint32_t *)ctx->code;
 
247
    int n = (int)(ctx->codesz >> 2);
 
248
    for (i = 0; i < n; i++, p++)
 
249
      *p = lj_bswap(*p);  /* Byteswap .text section. */
 
250
#else
 
251
    fprintf(stderr, "Error: different byte order for host and target\n");
 
252
    exit(1);
 
253
#endif
 
254
  }
239
255
  owrite(ctx, ctx->code, ctx->codesz);
240
256
  for (i = 0; i < ctx->nreloc; i++) {
241
257
    PEreloc reloc;
242
 
    reloc.vaddr = (uint32_t)ctx->reloc[i].ofs;
 
258
    reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS;
243
259
    reloc.symidx = 1+2+ctx->reloc[i].sym;  /* Reloc syms are after .text sym. */
244
260
    reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32;
245
261
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);