Contiki 3.x
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  */
30 
31 #ifdef __AVR__
32 #include <avr/pgmspace.h>
33 #endif
34 
35 #include "loader/sym.h"
36 
37 #include <string.h>
38 
39 static union sym_value
40 sym_lookup(const char *name, const struct sym_bol *symbols, int nelts)
41 {
42  union sym_value ret;
43  int start, middle, end;
44  int r;
45 
46  start = 0;
47  end = nelts - 1;
48 
49  while(start <= end) {
50  /* Check middle, divide */
51  middle = (start + end) / 2;
52 #ifdef __AVR__
53  PGM_P addr = (PGM_P)pgm_read_word(&symbols[middle].name);
54  r = strcmp_P(name, addr);
55 #else
56  r = strcmp(name, symbols[middle].name);
57 #endif
58  if(r < 0) {
59  end = middle - 1;
60  } else if(r > 0) {
61  start = middle + 1;
62  } else {
63 #ifdef __AVR__
64  ret.func = (sym_func_t)pgm_read_word(&symbols[middle].value);
65  return ret;
66 #else
67  return symbols[middle].value;
68 #endif
69  }
70  }
71  ret.obj = NULL;
72  ret.func = NULL;
73  return ret;
74 }
75 
76 /* Lookup a pointer to an ANSI C object. */
77 void *
78 sym_object(const char *name)
79 {
80  union sym_value ret;
81  ret = sym_lookup(name, sym_obj, sym_obj_nelts);
82  if(ret.obj != NULL)
83  return ret.obj;
84 
85  /*
86  * If the implementation puts constants into the text segment, this
87  * is where to find them!
88  */
89  ret = sym_lookup(name, sym_func, sym_func_nelts);
90  return ret.obj;
91 }
92 
93 /* Lookup a pointer to an ANSI C function. */
94 sym_func_t
95 sym_function(const char *name)
96 {
97  return sym_lookup(name, sym_func, sym_func_nelts).func;
98 }
#define NULL
The null pointer.