1/* Solaris host-specific hook definitions.
2   Copyright (C) 2004 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 2, 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 COPYING.  If not, write to the
18   Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include <sys/mman.h>
25#include "hosthooks.h"
26#include "hosthooks-def.h"
27
28
29#undef HOST_HOOKS_GT_PCH_USE_ADDRESS
30#define HOST_HOOKS_GT_PCH_USE_ADDRESS sol_gt_pch_use_address
31
32/* Map SIZE bytes of FD+OFFSET at BASE.  Return 1 if we succeeded at
33   mapping the data at BASE, -1 if we couldn't.  */
34
35static int
36sol_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
37{
38  void *addr;
39
40  /* We're called with size == 0 if we're not planning to load a PCH
41     file at all.  This allows the hook to free any static space that
42     we might have allocated at link time.  */
43  if (size == 0)
44    return -1;
45
46  addr = mmap (base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
47	       fd, offset);
48
49  /* Solaris isn't good about honoring the mmap START parameter
50     without MAP_FIXED set.  Before we give up, search the desired
51     address space with mincore to see if the space is really free.  */
52  if (addr != base)
53    {
54      size_t page_size = getpagesize();
55      char one_byte;
56      size_t i;
57
58      if (addr != (void *) MAP_FAILED)
59	munmap (addr, size);
60
61      errno = 0;
62      for (i = 0; i < size; i += page_size)
63	if (mincore ((char *)base + i, page_size, (void *)&one_byte) == -1
64	    && errno == ENOMEM)
65	  continue; /* The page is not mapped.  */
66	else
67	  break;
68
69      if (i >= size)
70	addr = mmap (base, size,
71		     PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
72		     fd, offset);
73    }
74
75  return addr == base ? 1 : -1;
76}
77
78
79const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;
80