Contiki 2.5
sym.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)$Id: sym.c,v 1.1 2007/04/25 15:33:54 bg- Exp $
30  */
31 
32 #ifdef __AVR__
33 #include <avr/pgmspace.h>
34 #endif
35 
36 #include "loader/sym.h"
37 
38 #include <string.h>
39 
40 static union sym_value
41 sym_lookup(const char *name, const struct sym_bol *symbols, int nelts)
42 {
43  union sym_value ret;
44  int start, middle, end;
45  int r;
46 
47  start = 0;
48  end = nelts - 1;
49 
50  while(start <= end) {
51  /* Check middle, divide */
52  middle = (start + end) / 2;
53 #ifdef __AVR__
54  PGM_P addr = (PGM_P)pgm_read_word(&symbols[middle].name);
55  r = strcmp_P(name, addr);
56 #else
57  r = strcmp(name, symbols[middle].name);
58 #endif
59  if(r < 0) {
60  end = middle - 1;
61  } else if(r > 0) {
62  start = middle + 1;
63  } else {
64 #ifdef __AVR__
65  ret.func = (sym_func_t)pgm_read_word(&symbols[middle].value);
66  return ret;
67 #else
68  return symbols[middle].value;
69 #endif
70  }
71  }
72  ret.obj = NULL;
73  ret.func = NULL;
74  return ret;
75 }
76 
77 /* Lookup a pointer to an ANSI C object. */
78 void *
79 sym_object(const char *name)
80 {
81  union sym_value ret;
82  ret = sym_lookup(name, sym_obj, sym_obj_nelts);
83  if(ret.obj != NULL)
84  return ret.obj;
85 
86  /*
87  * If the implementation puts constants into the text segment, this
88  * is where to find them!
89  */
90  ret = sym_lookup(name, sym_func, sym_func_nelts);
91  return ret.obj;
92 }
93 
94 /* Lookup a pointer to an ANSI C function. */
95 sym_func_t
96 sym_function(const char *name)
97 {
98  return sym_lookup(name, sym_func, sym_func_nelts).func;
99 }