OSDN Git Service

2010-04-30 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / config / host-solaris.c
1 /* Solaris host-specific hook definitions.
2    Copyright (C) 2004, 2007, 2008, 2010 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
6    GCC is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published
8    by the Free Software Foundation; either version 3, or (at your
9    option) any later version.
10
11    GCC is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14    License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include <sys/mman.h>
24 #include "hosthooks.h"
25 #include "hosthooks-def.h"
26
27
28 #undef HOST_HOOKS_GT_PCH_GET_ADDRESS
29 #define HOST_HOOKS_GT_PCH_GET_ADDRESS sol_gt_pch_get_address
30 #undef HOST_HOOKS_GT_PCH_USE_ADDRESS
31 #define HOST_HOOKS_GT_PCH_USE_ADDRESS sol_gt_pch_use_address
32
33 /* For various ports, try to guess a fixed spot in the vm space
34    that's probably free.  Based on McDougall, Mauro, Solaris Internals, 2nd
35    ed., p.460-461, fig. 9-3, 9-4, 9-5.  */
36 #if defined(__sparcv9__)
37 /* This low to avoid VA hole on UltraSPARC I/II.  */
38 # define TRY_EMPTY_VM_SPACE     0x70000000000
39 #elif defined(__sparc__)
40 # define TRY_EMPTY_VM_SPACE     0x80000000
41 #elif defined(__x86_64__)
42 # define TRY_EMPTY_VM_SPACE     0x8000000000000000
43 #elif defined(__i386__)
44 # define TRY_EMPTY_VM_SPACE     0xB0000000
45 #else
46 # define TRY_EMPTY_VM_SPACE     0
47 #endif
48
49 /* Determine a location where we might be able to reliably allocate
50    SIZE bytes.  FD is the PCH file, though we should return with the
51    file unmapped.  */
52
53 static void *
54 sol_gt_pch_get_address (size_t size, int fd)
55 {
56   void *addr;
57
58   addr = mmap ((caddr_t) TRY_EMPTY_VM_SPACE, size, PROT_READ | PROT_WRITE,
59                MAP_PRIVATE, fd, 0);
60
61   /* If we failed the map, that means there's *no* free space.  */
62   if (addr == (void *) MAP_FAILED)
63     return NULL;
64   /* Unmap the area before returning.  */
65   munmap ((caddr_t) addr, size);
66
67   return addr;
68 }
69
70 /* Map SIZE bytes of FD+OFFSET at BASE.  Return 1 if we succeeded at 
71    mapping the data at BASE, -1 if we couldn't.  */
72
73 static int
74 sol_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
75 {
76   void *addr;
77
78   /* We're called with size == 0 if we're not planning to load a PCH
79      file at all.  This allows the hook to free any static space that
80      we might have allocated at link time.  */
81   if (size == 0)
82     return -1;
83
84   addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
85                fd, offset);
86
87   /* Solaris isn't good about honoring the mmap START parameter
88      without MAP_FIXED set.  Before we give up, search the desired
89      address space with mincore to see if the space is really free.  */
90   if (addr != base)
91     {
92       size_t page_size = getpagesize();
93       char one_byte;
94       size_t i;
95
96       if (addr != (void *) MAP_FAILED)
97         munmap ((caddr_t) addr, size);
98
99       errno = 0;
100       for (i = 0; i < size; i += page_size)
101         if (mincore ((char *)base + i, page_size, (char *) &one_byte) == -1
102             && errno == ENOMEM)
103           continue; /* The page is not mapped.  */
104         else
105           break;
106
107       if (i >= size)
108         addr = mmap ((caddr_t) base, size, 
109                      PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
110                      fd, offset);
111     }
112
113   return addr == base ? 1 : -1;
114 }
115
116 \f
117 const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;